How can I fix my code to do line by line conditional statements in Matlab

91 views Asked by At

I just started learning Matlab roughly an hour ago after being told I had to convert my rscript into Matlab. I wrote a conditional statement below and it does not work. The reason I believe is because it is not currently going line by line. data is a Matlab table. Here is my current code:

if data.Year == data.initYear
    data.initY = -1
else
    data.initY = 0
end

in R it was:

workable$saleYear <- ifelse(workable$year == workable$initYear, -1,0)

Any help would be appreciated

1

There are 1 answers

0
user1543042 On

You want to do this in a vector. That way you don't need for loops or if statements.

data.initY = zeros(size(data.Year));
data.initY(data.Year == data.initYear) = -1;