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

1 次查看(过去 30 天)
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

更多回答(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