Hi Everyone, I need your help on my problem.
Mostrar comentarios más antiguos
I have a 3D matrix (abc), and I need to do if and elseif conditions to this matrix. However, the only the first expression(aa=abc-5) is applied to all elements. %
%
ab=[10,2,2;4,12,6;7,5,9];
abc=repmat(ab,1,1,3);
if 1<abc<=5
aa=abc-5;
elseif 6<abc<=10
aa=abc+10;
end
How can I make the elseif expression work?
Thank you.
Chane
Respuesta aceptada
Más respuestas (1)
Actually, you do not need a ifelse statement. Simple logical indexing will give you what you want.
ab=[10,2,2;4,12,6;7,5,9];
abc=repmat(ab,1,1,3);
abc(abc>1 & abc<=5)=abc(abc>1 & abc<=5)-5;
abc(abc>6 & abc<=10)=abc(abc>6 & abc<=10)+10
6 comentarios
Engdaw Chane
el 7 de Mzo. de 2018
Steven Lord
el 7 de Mzo. de 2018
The expression on the left side of the equals sign refers to only those elements in t20_50 whose corresponding elements in Hx20_50 are greater than 0 and less than or equal to 0.5. The expression on the right side operates on all the elements in Hx20_50.
Use the logical mask on both sides so you're working with the same sized "chunk" of t20_50 and Hx20_50.
ind = Hx20_50>0 & Hx20_50<=0.5;
t20_50(ind) = sqrt(log(1./(Hx20_50(ind).^2)));
Engdaw Chane
el 7 de Mzo. de 2018
Walter Roberson
el 7 de Mzo. de 2018
mask = 0<Hx20_50 & Hx20_50<=0.5;
t20_50(mask) = sqrt(log(1./(Hx20_50(mask).^2)));
mask = 0.5<Hx20_50 & Hx20_50<=1;
t20_50(mask) = sqrt(log(1./(1-Hx20_50(mask)).^2));
What do you want to do for the cases where Hx20_50 <= 0 or Hx20_50 > 1 ?
Engdaw Chane
el 7 de Mzo. de 2018
Editada: Engdaw Chane
el 7 de Mzo. de 2018
Walter Roberson
el 7 de Mzo. de 2018
If the inputs are certain to be in that range then you can simplify to
mask = Hx20_50 <= 0.5;
t20_50(mask) = sqrt(log(1./(Hx20_50(mask).^2)));
t20_50(~mask) = sqrt(log(1./(1-Hx20_50(~mask)).^2));
Categorías
Más información sobre Structures en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!