Hi Everyone, I need your help on my problem.

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

Jan
Jan el 7 de Mzo. de 2018
Editada: Jan el 7 de Mzo. de 2018
1 < abc <= 5
This will not do, what you expect. Matlab processes this expression from left to right:
  1. 1 < abc. This is either TRUE or FALSE, which is interpreted as 1 or 0
  2. 1 <= 5 or 0 <= 5. This is TRUE in both cases.
You want:
1 < abc && abc <= 5
Then your if and elseif will work.
For productive code using logical indexing as suggested by Birdman is usually more efficient and nicer. But understanding why a<b<c fails is essential.

3 comentarios

Jan Saimon, Thank you for your explanatory answer. However,I am getting the error ” Operands to the and && operators must be convertible to logical scalar values.” on the following.
% code
if (0<Hx20_50 && Hx20_50<=0.5);
t20_50=sqrt(log(1./(Hx20_50.^2)));
elseif (0.5<Hx20_50 && Hx20_50<=1);
t20_50=sqrt(log(1./(1-Hx20_50).^2));
end
Thank you.
If your Hx20_50 is a vector, you cannot use && but need &. But then the condition of the if command is a vector also. Because if requires a scalar condition, a all() is inserted automatically. This is most likely not, what you want.
Either use a loop:
t20_50 = zeros(size(Hx20_50)); % Pre-allocate!
for k = 1:numel(Hx20_50)
if 0<Hx20_50(k) && Hx20_50(k)<=0.5
t20_50(k) = sqrt(log(1 ./ (Hx20_50(k) .^ 2)));
elseif 0.5<Hx20_50(k) && Hx20_50(k)<=1
t20_50(k) = sqrt(log(1 ./ (1-Hx20_50(k)) .^ 2));
end
end
or use the logical indexing shown by Walter.
Engdaw Chane
Engdaw Chane el 7 de Mzo. de 2018
@Jan This was what I have been looking for. The others also work but I wasn't getting the dimensions I needed because of a reason in my code. Thank you.

Iniciar sesión para comentar.

Más respuestas (1)

Birdman
Birdman el 7 de Mzo. de 2018
Editada: Birdman el 7 de Mzo. de 2018
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

Birdman, Thank you for replying. The logical indexing works. But when I adapt the LOGICAL INDEXING to my matrix I am still getting error “In an assignment A(:) = B, the number of elements in A and B must be the same. “
% end
t20_50(Hx20_50>0 & Hx20_50<=0.5)=sqrt(log(1./(Hx20_50.^2)));
Where Hx20_50 is a 3D matrix, and t20_50 is the resulting matrix;
Can I use the LOGICAL INDEXING here? What went wrong in the above?
Kindly,
Chane
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)));
@Steven Lord, Thank you for helping. When I do so: 1, the dimension disappears.
2, I have four conditions to consider.
Please take a look at the whole code here.
% code
if (0<Hx20_50 && Hx20_50<=0.5);
t20_50=sqrt(log(1./(Hx20_50.^2)));
elseif (0.5<Hx20_50 && Hx20_50<=1);
t20_50=sqrt(log(1./(1-Hx20_50).^2));
end
Thank you
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
Engdaw Chane el 7 de Mzo. de 2018
Editada: Engdaw Chane el 7 de Mzo. de 2018
@Walter Hx20_50 is probability of elements, and the values are just between 0 and 1. just the dimensions are missing. Thank you.
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));

Iniciar sesión para comentar.

Categorías

Etiquetas

Preguntada:

el 7 de Mzo. de 2018

Comentada:

el 7 de Mzo. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by