How to use find function to get log of the values?
2 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Akiva Gordon
on 8 Nov 2012
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 Comments
More Answers (0)
See Also
Categories
Find more on Multidimensional Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!