matlab: represent 1 column data into 3 column data -


if 1st column detect 1, add  1 -1 -1 2nd 4th column  if 1st column detect 2, add -1  1 -1 2nd 4th column  if 1st column detect 3, add -1 -1  1 2nd 4th column  

example: 5x1 matrix

a= 1 2 3 2 1 

i result below: become 5x4 matrix

a = 1  1 -1 -1 2 -1  1 -1 3 -1 -1  1 2 -1  1 -1 1  1 -1 -1 

the code wrote below can not 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 vectors in if statements. won't work. need loop on entire vector, checking each element itself. preferable preallocate result matrix before modifying it, allocation expensive operation:

a = [a zeros(size(a,1), 3)]; i=1:size(a,1)     if(a(i,1) == 1)         a(i,2:4) = [1 -1 -1];     elseif(a(i,1) == 2)         a(i,2:4) = [-1 1 -1];     elseif(a(i,1) == 3)         a(i,2:4) = [-1 -1 1];     end end 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -