Retrieve loop counter values in nested loop, and plotting specific calculation

4 views (last 30 days)
Hello all. My questions are as commentated
count = 0 ;
vec = zeros([],7) ;
for a=1:3
A= 2*a ;
for b=a+1:6
B=3*b ;
for c=b+1:10
C=4*c ;
count = count+1 ;
total=sum([A B C])
vec(count,:)=[a b c A B C total];
end
end
end
value = max(total) % get max and return values of a,b,c when this happens
% plotting corresponding individual calculation A B C for the max(total)
end
  4 Comments
Adam Danz
Adam Danz on 15 Aug 2018
I don't follow your explanation. However, when I run your code I get an error on the first iteration since
[a b c A B C total]
has 10 elements but
vec(count,:)
expects 11 elements. On the 2nd iteration there are 11 elements in [a b c...] so there isn't an error.
Pit Probst
Pit Probst on 15 Aug 2018
Edited: Pit Probst on 15 Aug 2018
say,max(total) = 74. So the output of the code should be/needs to be:
value=74
a=2
b=3
c=5
A= [2 4]
B= [3 6 9]
C= [5 10 15 20]
vec = [A B C]
or: store each output of each iteration as new row in a matrix
vec=[a b c A B C value]
so vec=[2 4 3 6 9 5 10 15 20], and I can plot vec. I am somehow not storing the iterations correctly. Hence cannot retrieve values nor plot...

Sign in to comment.

Accepted Answer

dpb
dpb on 15 Aug 2018
Edited: dpb on 15 Aug 2018
I didn't notice the "growing" of the vector length...you've got to use a cell array to handle that.
total=[]; vec=[];
count=0;
for a=1:3
A= 2*a ;
for b=a+1:6
B=3*(1:b);
for c=b+1:10
C=4*(1:c);
count=count+1 ;
total(count)=sum([A B C]);
vec{count,1}=[sum([A B C]) a b c A B C]; % make column cell array
end
end
end
[value,ival]=max(total);
To retrieve the content use
vec{ival}
NB: that with the given positive value the maximum is always the last value so there's no need to search; one presumes this is artificial example.
One could also do the max() on the running total in the loop; there's no real reason to save anything except the maximum and conditions so far to get the actual result
  8 Comments
Pit Probst
Pit Probst on 15 Aug 2018
Edited: Pit Probst on 15 Aug 2018
Going back to your first code. vec{ival} works ! - and ill go with that. And yes, this code is an artificial example and part of a larger code in optimization. I can't post the real equations due to confidentiality. I can see why you'd think this the max value is a no-brainer as it is obviously at the end - didn't spend too much time dressing the equations.
The example immediately above was manually calculated while typing, to demonstrate the rationale for the problem. that's all.
Thanks for all your inputs! I was slightly pre-stressed about retrieving values, and missed the vec{ival}. But thanks a ton! I'll to do better!
dpb
dpb on 15 Aug 2018
OK...if wish, post a new Q? that illustrates your final output storage arrangement and what you want to retrieve and we can address the efficiencies regarding what, specifically, to actually store and how to retrieve what's needed for the real application.
I just introduced the total vector as an intermediary device to get to an answer while still trying to define the problem.
If it's so that you do want/need the subvectors then I recommend the probably solution is to save 2D cell array instead of 1D array of variable-length vector that combines the different pieces.

Sign in to comment.

More Answers (0)

Categories

Find more on Exponents and Logarithms 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!