Pass values to outside of loop after each iteration

14 次查看(过去 30 天)
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