MATLAB: How to change the entries in one column of a cell array based on the previous columns

cell arraychange entriesrow-wise

I have a cell array called P with 4 columns. I want to change the entries in the 4th column based on the values of the other columns. I want to do this row-wise. For example, let's say I have this cell array:
P =
5 0 0 0
4 2 2 0
5 3 2 0
5 0 0 0
1 0 2 0
2 0 0 0
4 2 0 0
2 0 2 0
My real cell array is 250×4 instead of 8×4. I now want to make a loop that does the following things:
  1. Put a 1 in the 4th column if the 2nd and 3th column are both 0
  2. Put a 2 in the 4th column if the 2nd column is greater than 0 and the 3th column is 2
  3. Put a 3 in the 4th column if the 2nd column is equal to 0 and the 3th column is 2
  4. Put a 4 in the 4th column if the 2nd column is greater than 0 and the 3th column is equal to 0
So, the loop should turn my cell array P into:
5 0 0 1
4 2 2 2
5 3 2 2
5 0 0 1
1 0 2 3
2 0 0 1
4 2 0 4
2 0 2 3
Hope this was clear! Thank you!

Best Answer

To accomplish this I would suggest using a for loop to go through each row, and then an if statement inside the loop to determine the value of the fourth column. It looks like you don't need to stack your if statements in any way, so you should be fine with a single if statement and multiple elseifs.

for k = 1:size(data,1);
 if data(k,2)==0 && data(k,3)==0;
  data(k,4) = 1;
 elseif ...