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

 Accepted Answer

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;

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 26 Apr 2018

Answered:

Rik
on 26 Apr 2018

Community Treasure Hunt

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

Start Hunting!