MATLAB Answers


please help, I want to make comparative loop

Asked by Brwa
on 24 May 2013

Hi everyone, I have 2 problems.

1-I want to plot a figure for each value of y its mean i should have 10 figures, i tried many cases but i only got 1 figure.

2- i want to get max(y) for each iteration. then max y for all iteration and i want to know the max y belong to which step.

M = eye(2,2);
C = [ 2 3; 1 5];
t = 1:1:10
for i = 1:1:10
     y = M * C
     plot (t, y)
     y1 = max(y)     
     y2 = max(y1(1,:)) %   scalar
     y3 = max(y2) %  this is the max among all values of y for all steps
                  %  for y3 i want to know its belong to which row in y2 ?
Thanks for your help, your help always appreciated



No products are associated with this question.

2 Answers

Answer by Walter Roberson
on 24 May 2013
 Accepted answer

1) Before your plot() call, insert



In the loop,

y2(i) = max(y1(1,:));

After the loop,

[y3, y3row] = max(y2);


Image Analyst
on 25 May 2013

But you're not changing C inside the loop - it's the same every single iteration.

on 25 May 2013

so, how can i change it ? Sorry Im new in matlab.

in the example i just right down one matrix for C but actually i got C from another loop so its more than one matrix. does this program always use one of C matrices or each time will change C?

Image Analyst
on 29 May 2013

The way you wrote the code, there is only one C so it will use only that one C.

Answer by Brwa
on 24 May 2013
 Thank you  Mr Walter Roberson, both of them works well.

thanks alot, wish you the best


Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi test

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!