How do I save my answers with 2 for loops.

1 次查看(过去 30 天)
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.

采纳的回答

Stephen23
Stephen23 2016-2-11
编辑:Stephen23 2016-2-11
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

更多回答(1 个)

Walter Roberson
Walter Roberson 2016-2-11
  1 个评论
Dave L
Dave L 2016-2-11
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by