How to do the following two for loop?
i=0.1:0.1:0.9 ;
M=[1 1 1 1 1 1];
first outer loop :
In the 1st inner loop Assign i=0.9 on the first element, then in the 2nd inner loop Assign 0.8 on the first element till 0.1
then 2nd outer loop
In the 1st inner loop Assign i=0.9 on the 2nd element, in the 2nd inner loop Assign 0.8 on the 2nd element till 0.1
.
.
and so on .. till the 6th outer loop

 採用された回答

Torsten
Torsten 2023 年 2 月 9 日

0 投票

You mean
i=0.1:0.1:0.9 ;
M = [i.',ones(numel(i),5)]
M = 9×6
0.1000 1.0000 1.0000 1.0000 1.0000 1.0000 0.2000 1.0000 1.0000 1.0000 1.0000 1.0000 0.3000 1.0000 1.0000 1.0000 1.0000 1.0000 0.4000 1.0000 1.0000 1.0000 1.0000 1.0000 0.5000 1.0000 1.0000 1.0000 1.0000 1.0000 0.6000 1.0000 1.0000 1.0000 1.0000 1.0000 0.7000 1.0000 1.0000 1.0000 1.0000 1.0000 0.8000 1.0000 1.0000 1.0000 1.0000 1.0000 0.9000 1.0000 1.0000 1.0000 1.0000 1.0000
?

9 件のコメント

M
M 2023 年 2 月 9 日
編集済み: M 2023 年 2 月 9 日
@Torsten nope, I want to do the following:
first outer loop :
perform i=0.9 on the first element, then perform 0.8 on the first element till 0.1
then 2nd outer loop
perform i=0.9 on the 2nd element, then perform 0.8 on the 2nd element till 0.1
.
.
and so on .. till the 6th outer loop
Image Analyst
Image Analyst 2023 年 2 月 9 日
What does it mean to "perform i=0.9 on the first element,"???
Do you mean "Assign i=0.9 to the first element"?
M
M 2023 年 2 月 9 日
Yes assign @Image Analyst
Torsten
Torsten 2023 年 2 月 9 日
編集済み: Torsten 2023 年 2 月 9 日
How to do the following two for loop? and save the results in every iteration?
And what are the results ? You mean: save the 9*6=54 M-vectors in a big matrix of size 54x6 ?
M
M 2023 年 2 月 10 日
@Torsten, nope I meant the results of the rest of the program in each itertation
Torsten
Torsten 2023 年 2 月 10 日
編集済み: Torsten 2023 年 2 月 10 日
M = ones(1,6);
result = zeros(9,6);
for i = 1:6
for j = 9:-1:1
M(i) = 0.1*j;
M
% Do some computations and save result in matrix "result"
%result(j,i) = ...;
end
M(i) = 1.0;
end
M = 1×6
0.9000 1.0000 1.0000 1.0000 1.0000 1.0000
M = 1×6
0.8000 1.0000 1.0000 1.0000 1.0000 1.0000
M = 1×6
0.7000 1.0000 1.0000 1.0000 1.0000 1.0000
M = 1×6
0.6000 1.0000 1.0000 1.0000 1.0000 1.0000
M = 1×6
0.5000 1.0000 1.0000 1.0000 1.0000 1.0000
M = 1×6
0.4000 1.0000 1.0000 1.0000 1.0000 1.0000
M = 1×6
0.3000 1.0000 1.0000 1.0000 1.0000 1.0000
M = 1×6
0.2000 1.0000 1.0000 1.0000 1.0000 1.0000
M = 1×6
0.1000 1.0000 1.0000 1.0000 1.0000 1.0000
M = 1×6
1.0000 0.9000 1.0000 1.0000 1.0000 1.0000
M = 1×6
1.0000 0.8000 1.0000 1.0000 1.0000 1.0000
M = 1×6
1.0000 0.7000 1.0000 1.0000 1.0000 1.0000
M = 1×6
1.0000 0.6000 1.0000 1.0000 1.0000 1.0000
M = 1×6
1.0000 0.5000 1.0000 1.0000 1.0000 1.0000
M = 1×6
1.0000 0.4000 1.0000 1.0000 1.0000 1.0000
M = 1×6
1.0000 0.3000 1.0000 1.0000 1.0000 1.0000
M = 1×6
1.0000 0.2000 1.0000 1.0000 1.0000 1.0000
M = 1×6
1.0000 0.1000 1.0000 1.0000 1.0000 1.0000
M = 1×6
1.0000 1.0000 0.9000 1.0000 1.0000 1.0000
M = 1×6
1.0000 1.0000 0.8000 1.0000 1.0000 1.0000
M = 1×6
1.0000 1.0000 0.7000 1.0000 1.0000 1.0000
M = 1×6
1.0000 1.0000 0.6000 1.0000 1.0000 1.0000
M = 1×6
1.0000 1.0000 0.5000 1.0000 1.0000 1.0000
M = 1×6
1.0000 1.0000 0.4000 1.0000 1.0000 1.0000
M = 1×6
1.0000 1.0000 0.3000 1.0000 1.0000 1.0000
M = 1×6
1.0000 1.0000 0.2000 1.0000 1.0000 1.0000
M = 1×6
1.0000 1.0000 0.1000 1.0000 1.0000 1.0000
M = 1×6
1.0000 1.0000 1.0000 0.9000 1.0000 1.0000
M = 1×6
1.0000 1.0000 1.0000 0.8000 1.0000 1.0000
M = 1×6
1.0000 1.0000 1.0000 0.7000 1.0000 1.0000
M = 1×6
1.0000 1.0000 1.0000 0.6000 1.0000 1.0000
M = 1×6
1.0000 1.0000 1.0000 0.5000 1.0000 1.0000
M = 1×6
1.0000 1.0000 1.0000 0.4000 1.0000 1.0000
M = 1×6
1.0000 1.0000 1.0000 0.3000 1.0000 1.0000
M = 1×6
1.0000 1.0000 1.0000 0.2000 1.0000 1.0000
M = 1×6
1.0000 1.0000 1.0000 0.1000 1.0000 1.0000
M = 1×6
1.0000 1.0000 1.0000 1.0000 0.9000 1.0000
M = 1×6
1.0000 1.0000 1.0000 1.0000 0.8000 1.0000
M = 1×6
1.0000 1.0000 1.0000 1.0000 0.7000 1.0000
M = 1×6
1.0000 1.0000 1.0000 1.0000 0.6000 1.0000
M = 1×6
1.0000 1.0000 1.0000 1.0000 0.5000 1.0000
M = 1×6
1.0000 1.0000 1.0000 1.0000 0.4000 1.0000
M = 1×6
1.0000 1.0000 1.0000 1.0000 0.3000 1.0000
M = 1×6
1.0000 1.0000 1.0000 1.0000 0.2000 1.0000
M = 1×6
1.0000 1.0000 1.0000 1.0000 0.1000 1.0000
M = 1×6
1.0000 1.0000 1.0000 1.0000 1.0000 0.9000
M = 1×6
1.0000 1.0000 1.0000 1.0000 1.0000 0.8000
M = 1×6
1.0000 1.0000 1.0000 1.0000 1.0000 0.7000
M = 1×6
1.0000 1.0000 1.0000 1.0000 1.0000 0.6000
M = 1×6
1.0000 1.0000 1.0000 1.0000 1.0000 0.5000
M = 1×6
1.0000 1.0000 1.0000 1.0000 1.0000 0.4000
M = 1×6
1.0000 1.0000 1.0000 1.0000 1.0000 0.3000
M = 1×6
1.0000 1.0000 1.0000 1.0000 1.0000 0.2000
M = 1×6
1.0000 1.0000 1.0000 1.0000 1.0000 0.1000
M
M 2023 年 2 月 10 日
@Torsten, this didnt work
I want to do the following regardless the saving part...
How to do the following two for loop?
i=0.1:0.1:0.9 ;
M=[1 1 1 1 1 1];
first outer loop :
In the 1st inner loop Assign i=0.9 on the first element, then in the 2nd inner loop Assign 0.8 on the first element till 0.1
then 2nd outer loop
In the 1st inner loop Assign i=0.9 on the 2nd element, in the 2nd inner loop Assign 0.8 on the 2nd element till 0.1
.
.
and so on .. till the 6th outer loop
Torsten
Torsten 2023 年 2 月 10 日
編集済み: Torsten 2023 年 2 月 10 日
That's exactly what the code does (see above).
Or do you want the element of the outer loop remain at 0.1 instead of resetting it to 1 ?
Then delete the line
M(i) = 1.0;
M
M 2023 年 2 月 10 日
@Torsten yes i deleted that line and now its working good. Thanks

サインインしてコメントする。

その他の回答 (2 件)

Image Analyst
Image Analyst 2023 年 2 月 9 日

0 投票

Don't call variables "i" -- it's the imaginary constant.
Try this, for example to multiply M by 2
vec = 0.1:0.1:0.9
vec = 1×9
0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 0.7000 0.8000 0.9000
M = [vec 1 1 1 1 1]
M = 1×14
0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 0.7000 0.8000 0.9000 1.0000 1.0000 1.0000 1.0000 1.0000
result = M * 2
result = 1×14
0.2000 0.4000 0.6000 0.8000 1.0000 1.2000 1.4000 1.6000 1.8000 2.0000 2.0000 2.0000 2.0000 2.0000
% Or via a for loop
for k = 1 : numel(vec)
result(k) = 2 * M(k);
end
To learn other fundamental concepts, invest 2 hours of your time here:
Image Analyst
Image Analyst 2023 年 2 月 10 日

0 投票

"assign i=0.9 on the first element, then assign 0.8 on the first element till 0.1"
Try this
for k = 1 : 9
result(k) = (10 - k) * 0.1;
end
result
result = 1×9
0.9000 0.8000 0.7000 0.6000 0.5000 0.4000 0.3000 0.2000 0.1000

2 件のコメント

M
M 2023 年 2 月 10 日
I want to move into M=[i 1 1 1 1 1], it is an input for another function
Image Analyst
Image Analyst 2023 年 2 月 10 日
OK, so did you do it? Again, don't use "i" as the name of your variable. That is something everyone here advises against.
for k = 1 : 9
result(k) = (10 - k) * 0.1;
end
M = [result, 1 1 1 1 1]

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

M
M
2023 年 2 月 9 日

コメント済み:

M
M
2023 年 2 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by