필터 지우기
필터 지우기

How to use find function to get log of the values?

조회 수: 2 (최근 30일)
Patrick
Patrick 2012년 11월 8일
Hi,
I'm trying to create a new array B where the values are logs of array A (if greater than 1). If the value is smaller or equal to 1 I need to add 39 to these. So far I'm up to here but I don't know how to add that 39
A=[8 14 3;-13 1 42;-39 -8 15];
B=zeros(size(A));
l=find(A>1);
B(1:length(l))=log(A(l));
Thanks in advance

채택된 답변

Akiva Gordon
Akiva Gordon 2012년 11월 8일
Use logical indexing to accomplish this:
A = [8 14 3; -13 1 42; -39 -8 15];
B = zeros(size(A));
Valid indices are those in which A is greater than 1,
vi = logical(A>1);
Where we found a logical "true", take the log of that location in A,
B( vi) = log(A(vi));
For the other indices, add 39 to them,
B(~vi) = A(~vi)+39;
Is this the output you were expecting?
B =
2.0794 2.6391 1.0986
26.0000 40.0000 3.7377
0 31.0000 2.7081
  댓글 수: 2
Patrick
Patrick 2012년 11월 8일
Works! Thank you very much. I still have a lot to learn.
Akiva Gordon
Akiva Gordon 2012년 11월 8일
No problem, Patrick!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by