What is wrong with this code?
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Debmalya Pramanik
le 29 Juin 2015
Commenté : Image Analyst
le 29 Juin 2015
I have a image file, and I try to find the peaks using this command, and store each of them.
for i=1:1:1024
[pks(:,i) locs(:,i)] = findpeaks(double(gfileName(:,i)), 'NPeaks',3, 'MinPeakHeight',120);
end
why do I get an error: Subscripted assignment dimension mismatch.
Please help ASAP
1 commentaire
Réponse acceptée
Stephen23
le 29 Juin 2015
Modifié(e) : Stephen23
le 29 Juin 2015
The basic problem is that you are trying to assign vectors to columns of locs or pks, but they have a different number of rows to how many elements are in the vectors. For example if pks has three rows and you try to assign a vector of two elements then this throws an error.
Note that the documentation for findpeaks states that the option 'NPeaks' sets the "Maximum number of peaks to return", so there may actually be fewer than three peaks, i.e. the returned vectors may be shorter then three... that is the bug!
One solution is to use a temporary variable and only assign as many elements as it has, like this (untested):
N = 3;
pks = nan(N,1024);
loc = nan(N,1024);
for k = 1:1024
[P,L] = findpeaks(double(gfileName(:,k)), 'NPeaks',N, 'MinPeakHeight',120);
pks(1:numel(P),k) = P;
loc(1:numel(L),k) = L;
end
I also preallocated the arrays before the loop, and changed the name of the loop variable to k, rather then i which is the name of the imaginary unit.
1 commentaire
Image Analyst
le 29 Juin 2015
Other options are to just use the data immediately after you got it and don't worry about storing it for use later, after the for loop has ended, OR store the information in a cell array, which can take variable sized things, in the event that you need to store this information after your loop has ended.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Parametric Spectral Estimation 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!