help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: set zero to multiple values in a matrix


From: BOKU
Subject: Re: set zero to multiple values in a matrix
Date: Mon, 1 May 2017 10:26:19 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Icedove/45.6.0

Hello

Yes, you are right, in case if sel would be the indices this would work. However in my case sel represent the labels to be selected from the labeled matrix lab. Hence, I first would need to get the indices of all objects in lab defined by sel.

So basically

ind=find(lab==sel) does not work

ind = find(lab==sel(1));  gives ind for all elements labeled with 14

as find does not handle arrays of different sizes (op1 is 301x301, op2 is 1x6) I need to loop it because simply enough I just want to do find with all entries in lab defined in sel.

for i=sel(1:end)
t=find(lab==i);
matsel(t)=1;
endfor

would do the trick.

As I want to avoid looping as much as possible I wanted to know if there is a better way of handle the problem?

greets chris


On 2017-05-01 04:00, Nicholas Jankowski wrote:

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


reply via email to

[Prev in Thread] Current Thread [Next in Thread]