Replace values in an array in matlab without changing the original array -


my question given array a, how can give array identical except changing negatives 0 (without changing values in a)?

my way is:

b = a;

b(b<0)=0

is there one-line command , not requiring create copy of a?

while particular problem does happen have one-liner solution, e.g. pointed out luis , ian's suggestions, in general if want copy of matrix operation performed on it, way how did it. matlab doesn't allow chained operations or compound expressions, have no choice assign temporary variable in manner.

however, if makes feel better, b=a efficient not result in new allocated memory, unless / until b or change later on. in other words, before b(b<0)=0 step, b reference a , takes no memory. how matlab works under hood ensure no memory wasted on simple aliases.


ps. there nothing efficient one-liners per se; in fact, should avoid them if lead obscure code. it's better have things defined on multiple lines if makes logic , intent of algorithm clearer.

e.g, valid one-liner solves problem:

b = subsasgn(a, substruct('()',{a<0}), 0) 

this in fact literal answer question (i.e. pretty code matlab call under hood commands). clearer, more elegant code because it's one-liner? no, right?


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -