if 1st column detect 1, then add 1 -1 -1 to 2nd to 4th column
if 1st column detect 2, then add -1 1 -1 to 2nd to 4th column
if 1st column detect 3, then add -1 -1 1 to 2nd to 4th column
example: A is 5×1 matrix
A=
1
2
3
2
1
i would like to get the result as below: A become 5×4 matrix
A =
1 1 -1 -1
2 -1 1 -1
3 -1 -1 1
2 -1 1 -1
1 1 -1 -1
the code i wrote below can not get the above result, please help…
if A(1:end,1) == 1
A(1:end,2:4) = [1 -1 -1]
else if A(1:end,1) == 2
A(1:end,2:4) = [-1 1 -1]
else
A(1:end,2:4) = [-1 -1 1]
end
Firstly, you’re comparing integers to vectors in your
ifstatements. That won’t work. You need to loop over the entire vector, checking each element by itself. It is also preferable to preallocate the result matrix before modifying it, as allocation is an expensive operation: