Quantcast

set zero to multiple values in a matrix

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

set zero to multiple values in a matrix

BOKU
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?

greets chris


_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: set zero to multiple values in a matrix

NJank
On Apr 30, 2017 6:52 PM, "BOKU" <[hidden email]> 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

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: set zero to multiple values in a matrix

BOKU

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" <[hidden email]> 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


_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: set zero to multiple values in a matrix

NJank


On May 1, 2017 4:26 AM, "BOKU" <[hidden email]> wrote:

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. 


Ahh, my mistake. Maybe something like:

Ind=find(ismember(lab,sel))

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: set zero to multiple values in a matrix

BOKU

Thank you very much!

that is working perfectly!

Even better since ismember(lab,sel) returns a logical mask matrix! :-) and that is exactly what i need!

cheers chris

On 2017-05-01 12:10, Nicholas Jankowski wrote:


On May 1, 2017 4:26 AM, "BOKU" <[hidden email]> wrote:

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. 


Ahh, my mistake. Maybe something like:

Ind=find(ismember(lab,sel))


_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Loading...