What is wrong with this code?

2 visualizaciones (últimos 30 días)
Debmalya Pramanik
Debmalya Pramanik el 29 de Jun. de 2015
Comentada: Image Analyst el 29 de Jun. de 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 comentario
Stephen23
Stephen23 el 29 de Jun. de 2015
Editada: Stephen23 el 29 de Jun. de 2015
@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 :)

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 29 de Jun. de 2015
Editada: Stephen23 el 29 de Jun. de 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 comentario
Image Analyst
Image Analyst el 29 de Jun. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Parametric Spectral Estimation en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by