What is wrong with this code?

조회 수: 2 (최근 30일)
Debmalya Pramanik
Debmalya Pramanik 2015년 6월 29일
댓글: Image Analyst 2015년 6월 29일
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
Stephen23
Stephen23 2015년 6월 29일
편집: Stephen23 2015년 6월 29일
@Debmalya Pramanik: When you ask question please format your code using the {} Code button that you will find above the textbox. This time I did it for you :)

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2015년 6월 29일
편집: Stephen23 2015년 6월 29일
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
Image Analyst
Image Analyst 2015년 6월 29일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Parametric Spectral Estimation에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by