What is wrong with this code?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Debmalya Pramanik
am 29 Jun. 2015
Kommentiert: Image Analyst
am 29 Jun. 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 Kommentar
Akzeptierte Antwort
Stephen23
am 29 Jun. 2015
Bearbeitet: Stephen23
am 29 Jun. 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 Kommentar
Image Analyst
am 29 Jun. 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.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Parametric Spectral Estimation finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!