Effacer les filtres
Effacer les filtres

Output matrix dimension mismatch in for loop

1 vue (au cours des 30 derniers jours)
Catherine
Catherine le 27 Fév 2017
Commenté : Catherine le 27 Fév 2017
Hi guys, I am having problem with storing the peaks of my data in a for loop. It said subscripted assignment dimension mismatch. I am guessing may be the number of peaks is not the same for all the values in the loop. Here is a simplified version of my codes:
q = 0;
g= 0;
for A = 0:pi/2:4*pi;
for B = 1:1:10;
y(q+1) = sin(A*B);
H(q+1) = B;
q = q+1;
end
q = 0;
C(g+1,:) = y;
[pks,locs] = findpeaks(C(g+1,:));
D(g+1,:) = pks;
E(g+1,:) = B(locs);
g = g+1;
end
plot(H,y)
g = 0;
How can I store my peaks and the corresponding x values?
Thanks guys!

Réponse acceptée

Jan
Jan le 27 Fév 2017
Modifié(e) : Jan le 27 Fév 2017
Please post the complete error message, such that we do not have to guess the line, which causes the problem. I guess boldly:
C(g+1,:) = y;
Here y is a [1, q+1] row vector. Assigning it to a row of C requires, the C has the same number of columns. But this cannot be true over multiple iterations.
You did not include comments in the code. Without a description of what you want to achieve, suggestion a solution is an educated guess only.
[EDITED] The number of peaks differ between the iterations (I guess), then store them in a cell array:
Av = 0:pi/2:4*pi;
D = cell(1, numel(Av));
for A = Av
...
[pks,locs] = findpeaks(C(g+1,:));
D{g+1} = pks;
...
  3 commentaires
Jan
Jan le 27 Fév 2017
See [EDITED] in my answer.
Catherine
Catherine le 27 Fév 2017
Hi Jan, thanks so much for that. That worked perfectly! Just one last thing, is it possible to convert the cell array back to matrix with each entry occupying a new row? I have tried cell2mat but that just change it to a row vector with all the results which I can't identify which results belong to which iteration.The following is my results:
[] [1,1] [3.67394039744206e-16,6.12323399573677e-16,8.57252759403147e-16,1.10218211923262e-15] [1,1] [] [1,1] [1.10218211923262e-15,5.38968387752153e-15,-9.80955400591059e-16,6.85926003649836e-15] [1,1] []
I know the different length of the results will be a problem. Can it fill the empty cell with zeros to make up the same length? Something like the following matrix:
0 0 0 0
1 1 0 0
3.67394039744206e-16 6.12323399573677e-16 8.57252759403147e-16 1.10218211923262e-15
1 1 0 0
0 0 0 0
....... so on

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Shifting and Sorting Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by