matlab - Logical indexing creates row instead of column -


i failing understand behaviour of matlab i've discovered. here code:

ix = logical([1; 0; 1]); value = [2; 2; 2]; newvalue(ix) = value(ix) 

it outputs

2 0 2 

while expect

2 0 2 

i can “correct” adding newvalue = nan(size(value)) before code. understand why matlab creates row column vectors.

it behavior because when create array using indexing: a(1) = 2; a(2) = 3;, matlab (and octave) create row (a=[2, 3]). , when use logical indexing use method. in case matlab executes

newvalue(1) = value(1);  newvalue(3) = value(3); 

but @irreducible says, can avoid pre-allocating newvalue

and can obtain same results without using logical indexing with:

ix = [1; 0; 1]; value = [2; 2; 2]; newvalue = value.*ix; 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -