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 :-)

 Risposta accettata

Rik
Rik il 26 Apr 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;

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Richiesto:

il 26 Apr 2018

Risposto:

Rik
il 26 Apr 2018

Community Treasure Hunt

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

Start Hunting!

Translated by