Setting a logical matrix to a number

3 visualizaciones (últimos 30 días)
Frances Roberts
Frances Roberts el 21 de Mzo. de 2019
Comentada: dpb el 22 de Mzo. de 2019
Sorry if this has been posted before - but I couldn't find anything similar!
I have a matrix 176x80, and I've found the local max values which has given me a logical array of the same size
Now I want to change each true value to a certain number on a scale. I defined for the vector for this, and thought I'd need to use a loop - the loop technically works but not in the way I want;
for k=1:14080
if TF_com(k) >= 0.1
TF_com(k)=wavenumber(k);
else TF_com(k)=0;
end
end
Essentially, I want each true value in TF_com to become a wavenumber. However, when I run the code instead of listing the true values in order of wavenumber i.e.
true = wavenumber(1), false = 0, false = 0, true = wavenumber(2)
the loop makes the false values a wavenumber but still shows them as zero
I've tried to add a loop inside, but most of the time I get the error 'arrays don't match in size'
If it helps I know that there are 806 true values, where each one is a different wavenumber
Thanks!!!

Respuesta aceptada

dpb
dpb el 21 de Mzo. de 2019
A logical array is either 0 or 1...if you put something else in an element, then the array can no longer be class logical
You can do the assignment just using the logical addressing vector, but result will have to be a numeric array.
WN_ary=zeros(size(TF_com)); % Preallocate
idxNZ=find(TF_com); % the nonzero locations
WN_ary(idxNZ)=wavenumber(idxNZ); % store the NZ location values
  2 comentarios
Frances Roberts
Frances Roberts el 21 de Mzo. de 2019
Thank you!!
I think I orginally tried to do this but got a bit confused! I made a slight modification so the numbers would go in order, but otherwise it works!!
WN_ary=zeros(size(TF_com));
idxNZ=find(TF_com);
WN_ary(idxNZ)=wavenumber(1:806,1);
dpb
dpb el 22 de Mzo. de 2019
That's NOT the same solution as what you wrote originally...that doesn't put the wave number associated with the location in the location but just the 1:N values.
If that is what you really intend, ok, but make sure it really is the correct solution. And, if it that is so, don't bury "magic numbers" like 806 in the code, use something like
WN_ary(idxNZ)=wavenumber(1:numel(idxNZ));
instead so you don't have to change the code if the data change.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by