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

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

[vmax imax] = max(vec(:,end)) % get max and return values of a,b,c when this happens
vec(imax,:) contains the desired quantities.
Not sure what you want to do regarding a plot for a single point?
Hi, sorry for the ambiguity. With example : we can take this as a bag of nuts-mix being maximized for profits. loop counter represent the maximum constraint i.e max 3 units of type a, max 5 of type b and remainder of type c. updated the loop counter to reflect this
A, B, C are the resulting profits.
say, the maximum profit = 74 and it occurs at a=2, b=3,c=5. Then corresponding values for A= [2 4], B= [3 6 9] and C= [5 10 15 20] I want to plot this as a vector= [2 4 3 6 9 5 10 15 20].
If all values are stored as a matrix, at the end of each loop iteration, then it is possible to extract the elements (and any other just as well), and get a full picture - I unable to do this. All of it.
here is my code with updated loop counters:
count = 1 ;
vec = zeros([],11) ;
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) ;
total=sum([A B C]);
vec(count,:)=[a b c A B C total]. % gave a you have 10 on LHS but 11 on RHS , assignment error
count = count+1 ;
end
end
end
value = max(total);
end
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.
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

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

hi dpb, ok updating loop counters. (just fyi, I explain my problem statement in the answer above)
count = 1 ;
vec = zeros([],[]) ;
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) ;
vec(count,:)= [sum([A B C]) A B C a b c]
count = count+1 ;
end
end
end
[vmax imax]= max(vec(:,1))
disp(imax) % can this be modified to display the entire row?
this gives me the error:
vec =
35 2 3 6 4 8 12 1 2 3
Unable to perform assignment because the size of the left side is 1-by-10 and the size
of the right side is 1-by-11.
>>
how do I fix this and How do I make it display the entire row where this max value is?
That's not my code...run the code in the Answer instead exactly as it's given.
You're still trying to treat vec as an array and it can't be to save variable-length vectors by row.
Then, you'll have to dereference it with "the curlies" {} to return the content also as shown.
ok,ran it. both your code and my previously updated code result in the same answer so that's cool and working.
As you spotted, I do want to create that array so I can extract the row that has the values resulting in the max(vec), so I can plot that and this is what I'm not able to figure.
I showed how to dereference vec in the Answer. I still isn't clear what is to be plotted from a single point, however?
You've got a list of values in there plus the total as the last element in the vector. What's not stored is what elements of the vector{i} are from which subcomponent of the multiple loops; that can be figured out by looking for the locations where the change in value from one to next is negative but if the idea is to know how many of each of the elements is in the answer, the storage allocation isn't all that conducive to retrieving that, would like be better to have a 2D cell array, the second being one for each dimension/nesting level.
ok. I will check my code.
ill try with example, if this helps my case: say, max(vec) = 74 and it occurs at a=2, b=3,c=5. Then corresponding values for A= [2 4], B= [3 6 9] and C= [5 10 15 20].
what I'm going for is know the loop index values of max (vec) a,b,c . But plot only the vector [2 4 3 6 9 5 10 15 20]
Why don't you just adopt the (working) code I provided or indicate what is wrong with it so we have a common working point going forward?
The logic above doesn't return the combination that sums to 74
>> find(total==74)
ans =
1×0 empty double row vector
>>
A is never more than one element and putting in a diagnostic statement shows
...
if a==2 & b==3 & c==5
disp([num2str([count a b c]) ' A=' num2str(A) ' B=' num2str(B) ' C=' num2str(C)])
end
...
32 2 3 5 A=4 B=3 6 9 C=4 8 12 16 20
>>
when run the script and
>> total(32)
ans =
82
>>
If you get a result other than that, there's a logic difference to be uncovered.
Owing to the variable-length of vec, keeping the total is quite a lot simpler in order to do the max search; it can be done but let's get the algorithm and desired result nailed down before worrying about that...
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!
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

Asked:

on 14 Aug 2018

Commented:

dpb
on 15 Aug 2018

Community Treasure Hunt

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

Start Hunting!