Simple question about if statement

Hi all! I have a simple question, but I am new to matlab and having difficulty.
I have a column vector of 18000 or so values which are either 1 or 2, representing conditions of an experiment I am running. I need to create a second column vector with the opposite value: so if the number is a 1 in the first column, it needs to be a 2 in the second column (and vice versa).
So far I have:
if allorient(:,1)==1
allorient(:,2)=2
else
allorient(:,2)=1
end
But obviously this is not working and producing a second column of values with only one value. The statement obviously needs to go through each of the values in the first column and then use this to post the alternative value in the second column.
Thanks in advance if someone could help me :-)

 Respuesta aceptada

Rik
Rik el 26 de Abr. de 2018
You should use logical indexing, like in the example below, or use a loop.
LogicalIndexing=allorient(:,1)==1;
allorient( LogicalIndexing,2)=2;
allorient(~LogicalIndexing,2)=1;

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 26 de Abr. de 2018

Respondida:

Rik
el 26 de Abr. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by