Creating results from multiple for loops
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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
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.
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]}
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(:));
@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.
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.
"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
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]}
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!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)

