How to concatenate or save iteration from a loop when the answers are vectors?

I'm very new to MatLab and need some help with saving iterations from a loop. I know that when the answers from a loop are scalar, you can make an array of answers. However, is there any way to do this when the answers from your loop are a vector? Here is my code:
Curve = input('What is your data: ' );
plot (Curve(1,1:end))
n = 0;
while n < size(Curve,1)
n = n+1;
TS(n) = max(Curve(n,:));
end
% Yield Point
k = 0;
while k < size(Curve,1)
k = k+1;
[obs,YP_1] = findpeaks(Curve(k,:));%in MatLab 2013b
c = (length(YP_1)-1);
Y_P(k) = YP_1(c)+1;
end
% Elasticity
b = 0;
a = 0;
while b < size(Curve,1)
b = b+1;
YP(b) = Curve(b,Y_P)
end
Since Y_P is a vector, I am getting my answer for YP as a vector which is what I want. But each vector gets overwritten with the next iteration and I need all the data saved. I would really appreciate the help! Thank you!

2 Commenti

Can you give an example for data that is supposed to be inputted for "Curve"?
I'm currently working with a 18x3509 matrix which is actually a concatenation of two matrices of different dimensions.

Accedi per commentare.

 Risposta accettata

add an extra dimension
So instead of
Answer(i)= ....
write
Answer(i,j)= ....
where j is the loop index.
If the size is changing between each loop iteration you can also use cell arrays.
NOTE: Your code is all jammed up I can't really see what is what. Try to use "{} Code" when posting so your code is readable.

4 Commenti

Curve = input('What is your data: ' );
plot (Curve(1,1:end))
n = 0;
while n < size(Curve,1)
n = n+1;
TS(n) = max(Curve(n,:));
end
% Yield Point
k = 0;
while k < size(Curve,1)
k = k+1;
[obs,YP_1] = findpeaks(Curve(k,:));%in MatLab 2013b
c = (length(YP_1)-1);
Y_P(k) = YP_1(c)+1;
end
% Elasticity
b = 0;
a = 0;
while b < size(Curve,1)
b = b+1;
YP(b) = Curve(b,Y_P)
end
Sorry! That's my code, I tried to do that before but Matlab gives me this error:
Subscripted assignment dimension mismatch.
How would I go about making it into a cell array if I'm saving the an iteration of the same variable?
Ok couple of things here:
(1) This part of your code:
n = 0;
while n < size(Curve,1)
n = n+1;
TS(n) = max(Curve(n,:));
end
Gives you the max value of each row. You can simplify the whole thing in one line
TS=max(Curve,[],2);
(2) Although this part of your code is perfectly correct:
k = 0;
while k < size(Curve,1)
k = k+1;
[obs,YP_1] = findpeaks(Curve(k,:));%in MatLab 2013b
c = (length(YP_1)-1);
Y_P(k) = YP_1(c)+1;
end
you can change it to:
for k=1: (size(Curve,1)-1)
[obs,YP_1] = findpeaks(Curve(k,:));%in MatLab 2013b
Y_P(k) = YP_1(end-1)+1;
end
NOTE: Why k<size(Curve,1)? This ignores the last row of data in Curve. so if you have 5 rows of data in Curve the last row is not processed. If this is a bug and you want all rows then change "for k=1: (size(Curve,1)-1)" to "for k=1:size(Curve,1)"
(3) What is the porpose of this part of the code?
b = 0;
a = 0;
while b < size(Curve,1)
b = b+1;
YP(b) = Curve(b,Y_P)
end
what is "a" doing here? it is not used.
You can change this to
for b=1: (size(Curve,1)-1)
YP(b,:) = Curve(b,Y_P);
end
This should work. as long as size(Y_P) is 1*N or N*1.
NOTE: again you are not processing the last row. If this is a bug, change the for-loop as mentioned above.
Thank you, thank you, thank you! Amazing answer, I truly appreciate your help!
you are welcome.
One note: actually k < size(Curve,1) was not ignoring the last row. That was correct (my mistake). So make sure that you use for k=1:size(Curve,1) every where to process all rows.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Graphics Performance in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by