How to create nested loop?

1 次查看(过去 30 天)
Hi
Currently i am writing a program in MATLAB. In the program i have some indexed values that i want to use for further analysis. The indexed vales under the variable name toggle(idx.maxpts) are as follows:
ans =
1.0e-08 *
0.0844
0.1368
0.3354
0.7585
In the program i want to create a nested loop where i want to apply some equations to the first values and apply some equations to the third and fourth values and save the results. Once i have the output i want to apply some other equation to get the desired result. Thank you, any help is highy appreciated.

采纳的回答

Voss
Voss 2022-2-4
A demonstration of nested for loops in MATLAB:
A = magic(5);
disp(A);
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
[m,n] = size(A);
for ii = 1:m
for jj = 1:n
disp(A(ii,jj));
end
end
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
  3 个评论
Voss
Voss 2022-2-4
eqn = { ...
@(x)x^2 ...
@(x)x^3-2*x ...
@(x)sin(x)+exp(-x) ...
@(x)cos(x)^2-abs(x) ...
@(x)cos(x)^2+sin(x)^2 ...
};
A = magic(5);
disp(A);
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
[m,n] = size(A);
for ii = 1:m
for jj = 1:n
disp(eqn{ii}(A(ii,jj)));
end
end
289 576 1 64 225 12121 115 329 2716 4064 -0.7385 -0.2769 0.4202 0.9129 -0.0089 -9.2960 -11.2879 -18.0225 -20.7000 -2.0199 1 1 1 1 1.0000
Kabit Kishore
Kabit Kishore 2022-2-4
Thank you Bengimin. You are of great help. Much appreciated.

请先登录,再进行评论。

更多回答(0 个)

类别

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