Setting a logical matrix to a number
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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!!!
0 comentarios
Respuesta aceptada
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
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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!