matlab - Problems with matrix indexing using for loop and if condition -


i indexing, follows:

for c=1:size(params,1)     d=1:size(data,2)         if params(c,8)==1             value1(c,d)=data(params(c,11),d);         elseif params(c,8)==2             value2(c,d)=data(params(c,11),d);         elseif params(c,8)==3             value3(c,d)=data(params(c,11),d);         end     end end 

the problems if have params(:,8)=1,3,1,3,2,3,1... value1 contain zeros in rows 2, 4, 5, 6, etc. these rows not have 1 in column 8 in params. similarly, value2 contains zeros in rows 1, 2, 3, 4, 6, 7... , value3 contain zeros in row 1, 3, 5, 7, .... tell me how index don't have 'gaps' of zeros in between rows? thanks!

edit; below sample dataset:

data (1080x15 double)

168 432 45  86 170 437 54  82 163 423 52  83 178 434 50  84 177 444 42  87 177 444 58  85 175 447 48  77 184 451 59  86 168 455 52  104 174 437 62  88 175 443 55  85 179 456 51  92 168 450 73  82 175 454 60  68 

params (72x12 double - interested in column 8 , 11 ) i'm showing column 8-11 sake of space:

1   10  15  1 3   12  16  16 2   10  15  32 3   12  16  47 1   8   14  63 2   10  15  77 2   8   14  92 3   10  15  106 1   12  16  121 3   8   14  137 2   10  15  151 

the expected output value1, value2, , value3 should 24x15. because there 15 columns in data , value 1, 2, 3 occur 24 times each in column 8 in params.

you can use bsxfun avoid for-loop (note not vertorizing):

value1 = bsxfun(@times,data(params(:,11),:),(params(:,8)==1)); value2 = bsxfun(@times,data(params(:,11),:),(params(:,8)==2)); value3 = bsxfun(@times,data(params(:,11),:),(params(:,8)==3)); 

but still gives results 0 rows. can remove zero-rows by:

value1(all(value1==0,2),:)=[]; value2(all(value2==0,2),:)=[]; value3(all(value3==0,2),:)=[]; 

you can use above commands remove zero-rows in results without using bsxfun. not loose transparency.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -