Pass values to outside of loop after each iteration
Show older comments
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 Comments
"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
on 5 Feb 2019
@klb could you give a sample of the desired output with given all the inputs?
Answers (2)
Bob Thompson
on 4 Feb 2019
0 votes
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 Comments
klb
on 5 Feb 2019
"How do I store the max value of each iteration to outside of the for loop? "
Read Bob Nbob's answer again: it clearlystates "...you should store them in an indexed variable", and gives you an example of how to do this:
[maxv(w),maxi(w)] = max(both_new(:,end)); % Inside the loop
Did you try that yet? Also, read this:
klb
on 5 Feb 2019
klb
on 5 Feb 2019
"You are Editing my own quesiton"
Yes, I formatted your code correctly for you, following the guidelines here:
If you had done this yourself other people would not need to do it for you. It is very simple: select the code text and then click the code button. Which is what I did.
"...getting onto other people's comments and replaying - editing that answer 3 times."
Interestingly I cannot recall doing that, nor is that recorded anywhere in the list of recent activities for this forum
Please check the recent activity list right now, and tell me if you can find any record of me "getting onto other people's comments and replaying - editing that answer 3 times."
Specifically:
- "getting onto other people's comments"
- "editing that answer 3 times"
Please show screenshots from the recent activities pages, which show me doing either of those things: editting other people's comments on this thread, or editting any answer on this thread. I certainly remember editting my own comment three times (and this was confirmed by the recent activity list).
"do we have aproblem here?"
Not as far as I am aware.
"and back at Stephen Cobeldick and yes, it is preallocated. "
Please show us your code. When I tried Bob Nbob's code (with preallocation), it returned these vectors, which contain all of the requested values:
>> maxv
maxv =
1200.0 1140.0 1120.0 1110.0 1104.0 1100.0 1097.1 1095.0 1093.3 1092.0 1090.9 1090.0 1089.2 1088.6 1088.0 1087.5 1087.1 1086.7 1086.3 1086.0
>> maxi
maxi =
48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48
klb
on 5 Feb 2019
Guillaume
on 5 Feb 2019
@klb, I would recommend you do not attack people trying to help you. It will only result in you not getting the help you seek.
klb
on 5 Feb 2019
"How do I store /pass/ the resulting max value and the vector that results it , of each iteration to outside of the for loop? "
By using Bob Nbobs answer. It worked perfectly when I tried it:
for w=1:20
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
both = [ d; y];
both_new = [both, sum(both,2)];
[maxv(w),maxi(w)]=max(both_new(:,end)); % <--- Bob Nbob's code
end
giving all of the output values in two vectors:
>> maxv
maxv =
1200.0 1140.0 1120.0 1110.0 1104.0 1100.0 1097.1 1095.0 1093.3 1092.0 1090.9 1090.0 1089.2 1088.6 1088.0 1087.5 1087.1 1086.7 1086.3 1086.0
>> maxi
maxi =
48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48
If those vectors are not what you expect, please explain what you are expecting the output to be like. You can store any non-scalar values in an ND array or a cell array, also using indexing.
klb
on 5 Feb 2019
Guillaume
on 5 Feb 2019
" Now, I am closing this.This question is clearly too complex for you to follow."
Well, having temper tantrums is not going to help. Usually, if you don't get the answer you want it's more likely to be done to your lack of clarity than anything else. Both Bob and Stephen have sufficient experience with matlab and these forums to solve complex problems.
In any case, your question is not complex. If anything, it shows your lack of experience in matlab (not a criticism, just a fact) so maybe you should pause and try to understand the answers you were given.
Guillaume
on 5 Feb 2019
With the example code in the question, it's very unclear why Bob answer is not acceptable. It does exactly what is asked.
Note that none of the loops are required. Another way to obtain the same answer in a more logical way would be:
ss = (1:24)'; tt = (1:24)'; %is there actually a difference between the two?
colvalues = 1:5;
w = permute(1:20, [1, 3 , 2]);
d = 2 * ss .* colvalues + ss ./ w; %calculate d for all ss and w values at once. No loop needed
y = 3 * tt .* colvalues + tt ./ w; %calculate y for all tt and w values at once. No loop needed
both = [d;y]
[maxv, maxi] = max(sum(both, 2), [], 1);
maxv = squeeze(maxv)
maxi = squeeze(maxi)
Again, this gives the same result as Bob's answer.
Bob Thompson
on 5 Feb 2019
@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.
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).
Categories
Find more on Number games in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!