What is wrong with this code?
4 views (last 30 days)
Show older comments
Debmalya Pramanik
on 29 Jun 2015
Commented: Image Analyst
on 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 Comment
Accepted Answer
Stephen23
on 29 Jun 2015
Edited: Stephen23
on 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 Comment
Image Analyst
on 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.
More Answers (0)
See Also
Categories
Find more on Parametric Spectral Estimation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!