How do I save my answers with 2 for loops.

1 view (last 30 days)
So I have a transfer function x= 1/(w+4m). m goes from 1 to 5 and w goes from 1 to 3. The first for loop is m and w is the loop inside. So basically I need to save my answer as the following array
x =[m1w1 m1w2 m1w3 m2w1 m2w2 m2w3 m3w1 m3w2 m3w3 m4......till m5w3]
the answer would be
x =[1/5 1/6 1/7 1/9 1/10 1/11.........1/23]
right now I have
y = zeros(1,15);
for m = 1:5
for w= 1:3
x = 1/((w+m*4));
y(m) = x;
end
end
I know it's wrong.. how do i fix it? Thank you.

Accepted Answer

Stephen23
Stephen23 on 11 Feb 2016
Edited: Stephen23 on 11 Feb 2016
Using bsxfun is faster and neater than using loops:
>> X = bsxfun(@(m,w)1./(w+4*m), 1:5, (1:3).');
>> X(:)
ans =
0.2
0.16667
0.14286
0.11111
0.1
0.090909
0.076923
0.071429
0.066667
0.058824
0.055556
0.052632
0.047619
0.045455
0.043478

More Answers (1)

Walter Roberson
Walter Roberson on 11 Feb 2016
y(m,w) = x;
  1 Comment
Dave L
Dave L on 11 Feb 2016
thanks, i tried this an hour ago, it works for my purpose as well. I just had to add
t=reshape(y',1,15) it converts it into horizontal array.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!