Creating results from multiple for loops
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
Hi all
I have a problem with creating results from multiple for loops. What I am looking to is to create a matrix of row and columns (A1 and A2 in my work below) then for every cell (which is basically a pair of A1 and A2) make a new equation U1 which contains this pair, and a new variable (a) which length is different from the original variables. Finally, I need to find the maximum value of all possible combinations.
I tried the following. I know that my work is correct up to the line of for k = 1...., then I am not sure how to create a new vector of results. In other words, what I need is, for every given pair of A1 and A2, calculate U for all values of a and then store these values in vectors.
---------------
A1 = 1:1:5;
A2 = 1:1:5;
a = 0:0.1:1;
for row = 1:length(A1)
for col = 1:length(A2)
for k = 1: length(a)
U(row,col) = (A1(row) + A2(col))*a(k);
end
end
end
--------------
I hope my question is clear. Many thanks in advance!
採用された回答
Dyuman Joshi
2023 年 9 月 8 日
Do you mean like this -
A1 = 0:1:5;
A2 = 0:1:5;
a = 0:0.1:1;
a = shiftdim(a,-1);
%or
%a = permute(a,[2 3 1]);
out = (A1+A2').*a;
m = max(out,[],'all')
m = 10
16 件のコメント
Michael Henry
2023 年 9 月 8 日

Michael Henry
2023 年 9 月 8 日
Thank you very much. I think this didn't answer my question, though. What I need is something like the attached image. for every pair we generated (cell), we need to sweep the a values (in orange).
This will be repated for all cells. Finally, we need to find the maximum value of the combinatnion of A1,A2, and a.
Moreover, please note the relation between A1, A2, and a is more complicated than what I have provided, but I put a simple relation here to make it eaiser for you guys,
Thanks again and looking forward for you help.
Dyuman Joshi
2023 年 9 月 8 日
The manner in which you want to store the data requires the use of cell arrays instead of numerical array.
Also, please clarify if the formula is -
U(row,col) = (A1(r) + A2(rr))*a(k);
or
U(row,col) = A1(r) + A2(rr)*a(k);
Michael Henry
2023 年 9 月 8 日
編集済み: Michael Henry
2023 年 9 月 8 日
Exactly! my problem is how to convert every cell into multiple cells whith size of a. How I can use cell arrays?
I corrected the equation. Sorry about that!
Like this?
%The data in the image starts at 1 for A1 and A2
A1 = 1:5;
A2 = 1:5;
a = 0:0.1:1;
n1 = numel(A1);
n2 = numel(A2);
%Preallocation
C = cell(n1,n2);
for row=1:n1
for col=1:n2
C{row,col} = (A1(row)+A2(col))*a;
end
end
C
C = 5×5 cell array
{[ 0 0.2000 0.4000 0.6000 … ]} {[ 0 0.3000 0.6000 0.9000 … ]} {[ 0 0.4000 0.8000 1.2000 … ]} {[0 0.5000 1 1.5000 2 2.5000 3 … ]} {[ 0 0.6000 1.2000 1.8000 … ]}
{[ 0 0.3000 0.6000 0.9000 … ]} {[ 0 0.4000 0.8000 1.2000 … ]} {[0 0.5000 1 1.5000 2 2.5000 3 … ]} {[ 0 0.6000 1.2000 1.8000 … ]} {[ 0 0.7000 1.4000 2.1000 … ]}
{[ 0 0.4000 0.8000 1.2000 … ]} {[0 0.5000 1 1.5000 2 2.5000 3 … ]} {[ 0 0.6000 1.2000 1.8000 … ]} {[ 0 0.7000 1.4000 2.1000 … ]} {[ 0 0.8000 1.6000 2.4000 … ]}
{[0 0.5000 1 1.5000 2 2.5000 3 … ]} {[ 0 0.6000 1.2000 1.8000 … ]} {[ 0 0.7000 1.4000 2.1000 … ]} {[ 0 0.8000 1.6000 2.4000 … ]} {[ 0 0.9000 1.8000 2.7000 … ]}
{[ 0 0.6000 1.2000 1.8000 … ]} {[ 0 0.7000 1.4000 2.1000 … ]} {[ 0 0.8000 1.6000 2.4000 … ]} {[ 0 0.9000 1.8000 2.7000 … ]} {[0 1 2 3.0000 4 5 6 7 8 9 10]}
Michael Henry
2023 年 9 月 8 日
Exactly! Thank you so much.. This really works.. :)
One last thing, please, How I can let it find the maximum value in each cell array and (more importantly) the maximum value between all values in all cells and it's location, please?
Dyuman Joshi
2023 年 9 月 8 日
編集済み: Dyuman Joshi
2023 年 9 月 11 日
%The data in the image starts at 1 for A1 and A2
A1 = 1:5;
A2 = 1:5;
a = 0:0.1:1;
n1 = numel(A1);
n2 = numel(A2);
%Preallocation
%To store the output
C = cell(n1,n2);
%To store the maximum value of each cell
m = zeros(n1,n2);
for row=1:n1
for col=1:n2
vec = (A1(row)+A2(col))*a;
C{row,col} = vec;
m(row,col) = max(vec);
end
end
%Maximum value in each cell
m
m = 5×5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
6 7 8 9 10
%Maximum of all values in C
%and the linear index of the cell it occurs in
[mall,idx] = max(m,[],'all')
mall = 10
idx = 25
%or use this syntax
[mall,idx] = max(m(:))
mall = 10
idx = 25
%Get the subscript indices for the maximum value
[r,c] = ind2sub(size(m),idx)
r = 5
c = 5
Michael Henry
2023 年 9 月 9 日
編集済み: Michael Henry
2023 年 9 月 9 日
Thank you for your help.The first part worked perfectly.
But when I added the line
[mall,idx] = max(m,[],'all')
after the two for loops (after line contains m in your code), I am getting the following error when I excute the code
----------------
Error using max
Dimension argument must be a positive integer scalar within indexing range.
---------
I searched about this error but I can't solve it. Can you please hep me with this too? I use 2014, if this makes any difference.
Thanks again!
Voss
2023 年 9 月 9 日
You must be using an older version of MATLAB that doesn't support the 'all' option. Do this instead:
[mall,idx] = max(m(:));
Stephen23
2023 年 9 月 9 日
@Michael Henry: perhaps you are using a MATLAB version prior to R2018b, which did not support the "all" option. So that syntax will instead throw an error.
Michael Henry
2023 年 9 月 11 日
Thank you all.
Michael Henry
2023 年 9 月 11 日
編集済み: Michael Henry
2023 年 9 月 11 日
Can I ask for one more favor, I need to check for every value in the arrays (which are 11 values in each cell) in all cells of C. If the value in this array is less than (let's say 2), then I need to set it's value to zero, but not all elements in that array cell.
The final code I am using now is:
-------------------
A1 = 1:5;
A2 = 1:5;
a = 0:0.1:1;
n1 = numel(A1);
n2 = numel(A2);
C = cell(n1,n2);
for row=1:n1
for col=1:n2
C{row,col} = (A1(row)+A2(col))*a;
end
end
----------
When I run the above code, I got the following arrays, so, how I can do what I described above, and finally, I need to know the location of the maximum value in C?

Thanks again.
Dyuman Joshi
2023 年 9 月 11 日
"then I need to set it's value to zero, but not all elements in that array cell."
Then what is the criteria to decide which values are to be set to zero and which values are not to be changed?
Suppose this is the input -
[1 2 0 3.5 1.2 0.56 1.8 0.69 4.20]
What should be the output?
"finally, I need to know the location of the maximum value in C?"
I answered this above here - https://in.mathworks.com/matlabcentral/answers/2018686-creating-results-from-multiple-for-loops#comment_2878291
Michael Henry
2023 年 9 月 11 日
Thank you for your time.
My criteria is simple, if the value of any entry is less than a specific value, (let's say 2.1), this entry would be set to zero and the other values stay the same. Under this criterion, the output you given would be like this:
[0 0 0 3.5 0 0 0 0 4.20]
This should be done for ALL arrays in all cells, as you know.
Many thanks.
A1 = 1:5;
A2 = 1:5;
a = 0:0.1:1;
n1 = numel(A1);
n2 = numel(A2);
%Preallocation
%To store the output
C = cell(n1,n2);
%To store the maximum value of each cell
m = zeros(n1,n2);
%Threshold for comparison, random value for example
thresh = 2.5;
for row=1:n1
for col=1:n2
vec = (A1(row)+A2(col))*a;
%Set values lower than threshold to be 0
vec(vec<thresh) = 0;
%Continue with assignment
C{row,col} = vec;
end
end
C
C = 5×5 cell array
{[ 0 0 0 0 0 0 0 0 0 0 0]} {[ 0 0 0 0 0 0 0 0 0 2.7000 3]} {[ 0 0 0 0 0 0 0 2.8000 3.2000 … ]} {[ 0 0 0 0 0 2.5000 3 3.5000 4 … ]} {[ 0 0 0 0 0 3 3.6000 4.2000 … ]}
{[ 0 0 0 0 0 0 0 0 0 2.7000 3]} {[ 0 0 0 0 0 0 0 2.8000 3.2000 … ]} {[ 0 0 0 0 0 2.5000 3 3.5000 4 … ]} {[ 0 0 0 0 0 3 3.6000 4.2000 … ]} {[0 0 0 0 2.8000 3.5000 4.2000 … ]}
{[0 0 0 0 0 0 0 2.8000 3.2000 … ]} {[ 0 0 0 0 0 2.5000 3 3.5000 4 … ]} {[ 0 0 0 0 0 3 3.6000 4.2000 … ]} {[0 0 0 0 2.8000 3.5000 4.2000 … ]} {[ 0 0 0 0 3.2000 4 4.8000 … ]}
{[0 0 0 0 0 2.5000 3 3.5000 4 … ]} {[ 0 0 0 0 0 3 3.6000 4.2000 … ]} {[0 0 0 0 2.8000 3.5000 4.2000 … ]} {[ 0 0 0 0 3.2000 4 4.8000 … ]} {[ 0 0 0 2.7000 3.6000 4.5000 … ]}
{[ 0 0 0 0 0 3 3.6000 4.2000 … ]} {[0 0 0 0 2.8000 3.5000 4.2000 … ]} {[ 0 0 0 0 3.2000 4 4.8000 … ]} {[ 0 0 0 2.7000 3.6000 4.5000 … ]} {[ 0 0 0 3.0000 4 5 6 7 8 9 10]}
Michael Henry
2023 年 9 月 11 日
Thank you so much. This was really helpful.. :)
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索
タグ
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
