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

2 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Akiva Gordon
Akiva Gordon il 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 Commenti
Patrick
Patrick il 8 Nov 2012
Works! Thank you very much. I still have a lot to learn.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Multidimensional Arrays in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by