Setting a logical matrix to a number
조회 수: 3 (최근 30일)
이전 댓글 표시
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
채택된 답변
dpb
2019년 3월 21일
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
dpb
2019년 3월 22일
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!