help-octave
[Top][All Lists]

## Re: set zero to multiple values in a matrix

 From: Nicholas Jankowski Subject: Re: set zero to multiple values in a matrix Date: Sun, 30 Apr 2017 22:00:42 -0400

On Apr 30, 2017 6:52 PM, "BOKU" <address@hiddenboku.ac.at> wrote:
Hello,

i would like to know if there is a fast way to select some items of a indexed matrix?

this is not working in my case ?!

sel =

[14   19   23   24   25   29]

lab is a 301x301 labeled matrix

numel(unique(lab))
ans =  47

I want to do something like

lab(lab==!sel)=0

do I really need to go through a loop for doing this?

no, but since you have indices off the ones you don't want, inverting may be easier using logical indexing.

See:

I'm guessing without octave open in front of me, but something like this should work.

idx = ones(size(lab))
idx(sel) = 0
lab(idx) = 0

That should create a logical index inverted from your linear index, and set all of those spots not in sel to 0. I can't remember if you need to cast idx as logical before the last line