필터 지우기
필터 지우기

Pass values to outside of loop after each iteration

조회 수: 14 (최근 30일)
klb
klb 2019년 2월 4일
편집: klb 2019년 7월 11일
hi,
%note: outputs are floating point values i.e 123.4567
for w=1:20
for ss=1:24
d (ss,:)= 2*ss*(1:5)+(ss/w); % some formula that results in 1x5 matrix f(ss,w).
end
for tt=1:24
y(tt,:) = 3*tt*(1:5)+(tt/w); %placeholder formula that results in 1x5 matrix f(tt,w)
end
both = [ d; y] %glue matrices
both_new = [both, sum(both,2)] % adding a total column
% to retrieve values
[maxv,maxi]=max(both_new(:,end))
end
QUESTIONS: How to pass the maximum values, with the full vector retrieved that results in that max value to outside of the loop after each iteration of w .
output mat with n no. of row would look like = [w, the vector elements , sum]
  댓글 수: 2
Stephen23
Stephen23 2019년 2월 5일
편집: Stephen23 2019년 2월 5일
"QUESTIONS: How to pass the maximum values, with the full vector retrieved that results in that max value to outside of the loop after each iteration of w"
You already use indexing to store values in y and d. You can do the same for the max outputs: use indexing.
"output mat with n no. of row would look like = [w, the vector elements , sum]"
What is n ? (it does not exist in your code)
What is the sum ? (this is not clear from your code)
madhan ravi
madhan ravi 2019년 2월 5일
@klb could you give a sample of the desired output with given all the inputs?

댓글을 달려면 로그인하십시오.

답변 (2개)

Bob Thompson
Bob Thompson 2019년 2월 4일
You cannot 'pass' the values out of the loop, but instead you should store them in an indexed variable. Then, after the loop has finished, you can access all of the results from an array.
[maxv(w),maxi(w)] = max(both_new(:,end)); % Inside the loop
If this isn't what you're looking for, then I'm going to ask you to rephrase your request, because I don't quite understand it.
  댓글 수: 16
Bob Thompson
Bob Thompson 2019년 2월 5일
@klb Having looked over the comments in here let me see if I have a clearer picture of what you're looking for. You originally mentioned you were looking for an array like this:
[w, the vector elements , sum]
You also mentioned this:
Code is working just I dont know how to save the iteration result so that i have a matrix which is maximum value out of each iteration.
From these two comments I'm guessing that you don't want to output the max values and vectors directly, but to combine them with other values from each loop? How does this look?
[maxv,maxi]=max(both_new(:,end))
output(w,:) = [w,maxi,maxv];
This will output a matrix with N rows that has the first column as your settings number, the second column as the index of the max value, and the final column as the max value. In your original question you mentioned 'sum' as a value you wanted to include. Was that referring to the max values, or was there something else you wanted to output?
If this doesn't work for you please feel free to reply again. It would help me greatly if you could explain a comparison to what I'm doing versus what you are looking for. I won't ask you to fully repeat the question again, as you have done that numerous times, but an explanation of what you're seeing us do with our suggestions, and how that doesn't fit what you want will be helpful to move forward with this.
klb
klb 2019년 2월 13일
편집: klb 2019년 7월 11일
@ Bob Nbob Hi. thank you. I was waiting to hear from you but others took over..
I got stressed with all that repeated explaing to others so had to leave from here. I worked it out and here it is. But mostly, thank you for manners in talking. And patience.
both = [ d ; y] %glue matrices
both_new = [both, sum(both,2)] % adding a total column
[maxv,max]=max(both_new(:,end))
res = [res; w, both_new(maxi,:)]
end

댓글을 달려면 로그인하십시오.


Stephen23
Stephen23 2019년 2월 5일
편집: Stephen23 2019년 2월 5일
Based on your comment "output mat with n no. of row would look like = [w, the vector elements , sum]", perhaps using a cell array does what you want:
N = 20;
C = cell(N,3);
for w = 1:N
for ss = 1:24
d(ss,:) = 2*ss*(1:5)+(ss/w);
end
for tt = 1:24
y(tt,:) = 3*tt*(1:5)+(tt/w);
end
vec = sum([d;y],2);
[mxv,idx] = max(vec);
C{w,1} = w;
C{w,2} = vec.';
C{w,3} = mxv;
end
M = cell2mat(C)
It stores w, the vector vec and mxv in a cell array. E.g. the first row contains w, the vector elements, and the maximum value for the first iteration:
>> C{1,1} % the loop index
ans = 1
>> C{1,2} % the vector
ans =
35 70 105 140 175 210 245 280 315 350 385 420 455 490 525 560 595 630 665 700 735 770 805 840 50 100 150 200 250 300 350 400 450 500 550 600 650 700 750 800 850 900 950 1000 1050 1100 1150 1200
>> C{1,3} % the max value
ans = 1200
After the loops you can optionally convert this to a numeric array using cell2mat, which for the first row looks like this:
>> M(1,:)
ans =
1 35 70 105 140 175 210 245 280 315 350 385 420 455 490 525 560 595 630 665 700 735 770 805 840 50 100 150 200 250 300 350 400 450 500 550 600 650 700 750 800 850 900 950 1000 1050 1100 1150 1200 1200
This corresponds exactly to what you requested: "[w, the vector elements , sum]" (except I am not sure about what you mean by that sum value, this is not clear from your question... but in any case, you can put any value into that cell array, so change as required).

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by