Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
find elements to include page indices

Subject: find elements to include page indices

From: Dave Brackett

Date: 23 Jul, 2008 12:24:02

Message: 1 of 17

I have a matrix of size [1,16,50] and want to find the
positions of the non-zero and zero elements within.

I have been using find but this only either gives linear
indices or row and column indices. I want it to include page
indices.

I'm sure this is quite simple to do but I can't seem to work
it out. Any help would be appreciated. Thanks.

Subject: find elements to include page indices

From: Ian Clarkson

Date: 23 Jul, 2008 13:06:02

Message: 2 of 17

"Dave Brackett" <davebrackett@hotmail.com> wrote in message
<g677t2$j92$1@fred.mathworks.com>...
> I have a matrix of size [1,16,50] and want to find the
> positions of the non-zero and zero elements within.
>
> I have been using find but this only either gives linear
> indices or row and column indices. I want it to include page
> indices.
>
> I'm sure this is quite simple to do but I can't seem to work
> it out. Any help would be appreciated. Thanks.

how do you define a page index? as in a printed page? or
does your 3d matrix have some relation to pages? i don't
think anybody can help you until you clarify what it is
you're looking to do.

Subject: find elements to include page indices

From: Dave Brackett

Date: 23 Jul, 2008 13:23:02

Message: 3 of 17

"Ian Clarkson" <ovoidkumquat@hotmail.com> wrote in message
<g67abq$h8d$1@fred.mathworks.com>...
> "Dave Brackett" <davebrackett@hotmail.com> wrote in message
> <g677t2$j92$1@fred.mathworks.com>...
> > I have a matrix of size [1,16,50] and want to find the
> > positions of the non-zero and zero elements within.
> >
> > I have been using find but this only either gives linear
> > indices or row and column indices. I want it to include page
> > indices.
> >
> > I'm sure this is quite simple to do but I can't seem to work
> > it out. Any help would be appreciated. Thanks.
>
> how do you define a page index? as in a printed page? or
> does your 3d matrix have some relation to pages? i don't
> think anybody can help you until you clarify what it is
> you're looking to do.

sorry, by page indices I mean with regards to a 3D matrix.
i.e. for a matrix of size [1,16,50] the first dimension is
the rows, the second is the cols, and the third is the
pages. It has nothing to do with physical pages of paper.

Subject: find elements to include page indices

From: Steven Lord

Date: 23 Jul, 2008 13:38:56

Message: 4 of 17


"Dave Brackett" <davebrackett@hotmail.com> wrote in message
news:g677t2$j92$1@fred.mathworks.com...
>I have a matrix of size [1,16,50] and want to find the
> positions of the non-zero and zero elements within.
>
> I have been using find but this only either gives linear
> indices or row and column indices. I want it to include page
> indices.
>
> I'm sure this is quite simple to do but I can't seem to work
> it out. Any help would be appreciated. Thanks.

Use FIND to obtain the linear indices then use IND2SUB to convert them into
row, column, and page subscripts.

--
Steve Lord
slord@mathworks.com

Subject: find elements to include page indices

From: Steven Lord

Date: 23 Jul, 2008 13:42:25

Message: 5 of 17


"Ian Clarkson" <ovoidkumquat@hotmail.com> wrote in message
news:g67abq$h8d$1@fred.mathworks.com...
> "Dave Brackett" <davebrackett@hotmail.com> wrote in message
> <g677t2$j92$1@fred.mathworks.com>...
>> I have a matrix of size [1,16,50] and want to find the
>> positions of the non-zero and zero elements within.
>>
>> I have been using find but this only either gives linear
>> indices or row and column indices. I want it to include page
>> indices.
>>
>> I'm sure this is quite simple to do but I can't seem to work
>> it out. Any help would be appreciated. Thanks.
>
> how do you define a page index? as in a printed page? or
> does your 3d matrix have some relation to pages? i don't
> think anybody can help you until you clarify what it is
> you're looking to do.

When dealing with arrays in MATLAB, the term 'page' generally refers to the
third dimension. For instance, if x is:

x = ones(1, 16, 50);
y = x(:, :, 6);

x has 1 row, 16 columns, and 50 pages, and y contains all the rows and
columns in the 6th page of x.

And to answer the follow-up question I anticipate: I don't think there's
any generally used corresponding terms for any higher dimensions.

--
Steve Lord
slord@mathworks.com

Subject: find elements to include page indices

From: Dave Brackett

Date: 23 Jul, 2008 14:38:04

Message: 6 of 17

> Use FIND to obtain the linear indices then use IND2SUB to
convert them into
> row, column, and page subscripts.

Ok, so for a matrix, x of size [1 16 50]:

linear_non0=find(x) % to find non zero elements (size [788 1])
linear_0=find(x==0) % to find zero elements (size [12 1])

[I J K]=ind2sub(linear_non0,[1 16 50])
[I0 J0 K0]=ind2sub(linear_0,[1 16 50])

however, this returns the error:
??? Error using ==> horzcat
CAT arguments dimensions are not consistent.

Error in ==> ind2sub at 30
  siz = [siz(1:nout-1) prod(siz(nout:end))];

I think this related to the second argument i.e. [1 16 50].
I have looked at the help but can't figure out what to put
for this argument. thanks.

Subject: find elements to include page indices

From: Ian Clarkson

Date: 23 Jul, 2008 15:10:03

Message: 7 of 17

"Dave Brackett" <davebrackett@hotmail.com> wrote in message
<g67foc$92q$1@fred.mathworks.com>...
> > Use FIND to obtain the linear indices then use IND2SUB to
> convert them into
> > row, column, and page subscripts.
>
> Ok, so for a matrix, x of size [1 16 50]:
>
> linear_non0=find(x) % to find non zero elements (size [788 1])
> linear_0=find(x==0) % to find zero elements (size [12 1])
>
> [I J K]=ind2sub(linear_non0,[1 16 50])
> [I0 J0 K0]=ind2sub(linear_0,[1 16 50])
>
> however, this returns the error:
> ??? Error using ==> horzcat
> CAT arguments dimensions are not consistent.
>
> Error in ==> ind2sub at 30
> siz = [siz(1:nout-1) prod(siz(nout:end))];
>
> I think this related to the second argument i.e. [1 16 50].
> I have looked at the help but can't figure out what to put
> for this argument. thanks.

i believe you have your inputs to the ind2sub switched: the
first argument is the size and the second argument are the
values you're interested in.

Subject: find elements to include page indices

From: Dave Brackett

Date: 23 Jul, 2008 16:23:02

Message: 8 of 17

> i believe you have your inputs to the ind2sub switched: the
> first argument is the size and the second argument are the
> values you're interested in.

ah yeah, thanks. I can now get the subscripts and have
assigned them to a variable, a, which is then of the size
[788 3] with rows like:

[1 2 1;
 1 6 1;
 1 9 1;
 1 10 1;
 1 11 1;
 1 12 1;
 ...etc]

I want to use the indices at each row to extract the
corresponding elements from another matrix, b, which is of
the size [90 16 50].

I tried to implement this as simply c=b(a) but I get c as of
the size [788 3] whereas it should be of the form [90 16 50].

If I write c=b(1,2,1) I can get a single element, but if i
write c=b([1 2 1]) i get 3 elements. Why is there a
difference between these 2 methods? I think this might be
the cause of this problem.

Thanks

Subject: find elements to include page indices

From: Ian Clarkson

Date: 23 Jul, 2008 17:15:05

Message: 9 of 17

"Dave Brackett" <davebrackett@hotmail.com> wrote in message
<g67lt6$4c9$1@fred.mathworks.com>...
> > i believe you have your inputs to the ind2sub switched: the
> > first argument is the size and the second argument are the
> > values you're interested in.
>
> ah yeah, thanks. I can now get the subscripts and have
> assigned them to a variable, a, which is then of the size
> [788 3] with rows like:
>
> [1 2 1;
> 1 6 1;
> 1 9 1;
> 1 10 1;
> 1 11 1;
> 1 12 1;
> ...etc]
>
> I want to use the indices at each row to extract the
> corresponding elements from another matrix, b, which is of
> the size [90 16 50].
>
> I tried to implement this as simply c=b(a) but I get c as of
> the size [788 3] whereas it should be of the form [90 16 50].
>
> If I write c=b(1,2,1) I can get a single element, but if i
> write c=b([1 2 1]) i get 3 elements. Why is there a
> difference between these 2 methods? I think this might be
> the cause of this problem.
>
> Thanks
>

when you access it in the second way, you're getting the
matrix b at the linear indices 1, 2, and 1. Your output will
be something like [num1 num2 num1]. Instead, you should
access it via the linear indices (the ones you found BEFORE
passing to ind2sub). if you use those indices and the
c=b([linearindices]) notation, it should work.

Subject: find elements to include page indices

From: Steven Lord

Date: 23 Jul, 2008 17:17:19

Message: 10 of 17


"Dave Brackett" <davebrackett@hotmail.com> wrote in message
news:g67lt6$4c9$1@fred.mathworks.com...
>> i believe you have your inputs to the ind2sub switched: the
>> first argument is the size and the second argument are the
>> values you're interested in.
>
> ah yeah, thanks. I can now get the subscripts and have
> assigned them to a variable, a, which is then of the size
> [788 3] with rows like:
>
> [1 2 1;
> 1 6 1;
> 1 9 1;
> 1 10 1;
> 1 11 1;
> 1 12 1;
> ...etc]
>
> I want to use the indices at each row to extract the
> corresponding elements from another matrix, b, which is of
> the size [90 16 50].

If the two matrices (the one from which you extract the locations and the
one into which you want to index) are the same size, don't convert the
linear indices into subscripts -- just use the linear indices directly.

If the two matrices are _not_ the same size, then I'd think a bit more about
the problem. That seems to me to be an apples and oranges type of
situation.

> I tried to implement this as simply c=b(a) but I get c as of
> the size [788 3] whereas it should be of the form [90 16 50].

You're performing linear indexing into b. The result you get out is the
same size and shape as the matrix of indices.

> If I write c=b(1,2,1) I can get a single element, but if i
> write c=b([1 2 1]) i get 3 elements. Why is there a
> difference between these 2 methods? I think this might be
> the cause of this problem.

The former is subscripted indexing. The latter is linear indexing.

--
Steve Lord
slord@mathworks.com

Subject: find elements to include page indices

From: Dave Brackett

Date: 24 Jul, 2008 07:45:04

Message: 11 of 17

> when you access it in the second way, you're getting the
> matrix b at the linear indices 1, 2, and 1. Your output will
> be something like [num1 num2 num1]. Instead, you should
> access it via the linear indices (the ones you found BEFORE
> passing to ind2sub). if you use those indices and the
> c=b([linearindices]) notation, it should work.


Ok, using linear indices c=b(linearindices) I get c to be a
matrix of the size [788 1]. b is [90 16 50] and so how can i
get c back into the form [90 16 50]? I have tried ind2sub on
c as follows:

d=ind2sub([90 16 50],real(c))

but the size of d is still [788 1]. any ideas? thanks for
your help so far.

Subject: find elements to include page indices

From: us

Date: 24 Jul, 2008 08:00:24

Message: 12 of 17

"Dave Brackett"
<SNIP looking for his/her bookmarks...

> I have a matrix of size [1,16,50] and want to find the
> positions of the non-zero and zero elements within...

one of the many solutions

% the data
     m=zeros([2,3,4]);
     m(1,1,1)=nan;
     m(2,2,3)=nan;
     m(2,3,4)=nan;
% the engine
     lix=find(m);
     ix=cell(1,ndims(m));
     [ix{1:end}]=ind2sub(size(m),lix);
     r=cat(2,ix{:});
% the result
     disp(r);
%{
     1 1 1
     2 2 3
     2 3 4
%}

us

Subject: find elements to include page indices

From: Dave Brackett

Date: 24 Jul, 2008 08:25:06

Message: 13 of 17

> % the data
> m=zeros([2,3,4]);
> m(1,1,1)=nan;
> m(2,2,3)=nan;
> m(2,3,4)=nan;
> % the engine
> lix=find(m);
> ix=cell(1,ndims(m));
> [ix{1:end}]=ind2sub(size(m),lix);
> r=cat(2,ix{:});
> % the result
> disp(r);
> %{
> 1 1 1
> 2 2 3
> 2 3 4
> %}

ok, and then how can I find the elements of another matrix
using the indices from this result as questioned in my
previous message in this thread. thanks.

Subject: find elements to include page indices

From: us

Date: 24 Jul, 2008 08:33:04

Message: 14 of 17

"Dave Brackett":
<SNIP simple question...

> ok, and then how can I find the elements of another matrix
> using the indices from this result...

...one of the (simple) solutions

% the data
     m=zeros([2,3,4]);
     m(1,1,1)=nan;
     m(2,2,3)=nan;
     m(2,3,4)=nan;
% the engine
     lix=find(m);
     ix=cell(1,ndims(m));
     [ix{1:end}]=ind2sub(size(m),lix);
     r=cat(2,ix{:});
% the result
     disp(r);
%{
     1 1 1
     2 2 3
     2 3 4
%}

     m(isnan(m))=-1;
     lix=sub2ind(size(m),ix{:});
     r=m(lix);
     disp(r);
%{
     -1
     -1
     -1
%}

us

Subject: find elements to include page indices

From: Dave Brackett

Date: 24 Jul, 2008 09:07:22

Message: 15 of 17

"us " <us@neurol.unizh.ch> wrote in message
<g69eo0$ntt$1@fred.mathworks.com>...
> "Dave Brackett":
> <SNIP simple question...
>
> > ok, and then how can I find the elements of another matrix
> > using the indices from this result...
>
> ...one of the (simple) solutions
>
> % the data
> m=zeros([2,3,4]);
> m(1,1,1)=nan;
> m(2,2,3)=nan;
> m(2,3,4)=nan;
> % the engine
> lix=find(m);
> ix=cell(1,ndims(m));
> [ix{1:end}]=ind2sub(size(m),lix);
> r=cat(2,ix{:});
> % the result
> disp(r);
> %{
> 1 1 1
> 2 2 3
> 2 3 4
> %}
>
> m(isnan(m))=-1;
> lix=sub2ind(size(m),ix{:});
> r=m(lix);
> disp(r);
> %{
> -1
> -1
> -1
> %}
>
> us


Ok I think i may have missed out a crucial bit of info to
achieve what I want, so for clarity to summarise, I want to:
1) get the positions of non 0 values from x
2) use these positions to find the corresponding elements of
b but retain their relative positions, with all other
elements replaced by zeros.

If possible, I would prefer to use the non-cells method from
earlier in this thread. Thanks for all your help guys.

Subject: find elements to include page indices

From: Loren Shure

Date: 24 Jul, 2008 12:08:16

Message: 16 of 17

In article <g69goa$bud$1@fred.mathworks.com>, davebrackett@hotmail.com
says...
> "us " <us@neurol.unizh.ch> wrote in message
> <g69eo0$ntt$1@fred.mathworks.com>...
> > "Dave Brackett":
> > <SNIP simple question...
> >
> > > ok, and then how can I find the elements of another matrix
> > > using the indices from this result...
> >
> > ...one of the (simple) solutions
> >
> > % the data
> > m=zeros([2,3,4]);
> > m(1,1,1)=nan;
> > m(2,2,3)=nan;
> > m(2,3,4)=nan;
> > % the engine
> > lix=find(m);
> > ix=cell(1,ndims(m));
> > [ix{1:end}]=ind2sub(size(m),lix);
> > r=cat(2,ix{:});
> > % the result
> > disp(r);
> > %{
> > 1 1 1
> > 2 2 3
> > 2 3 4
> > %}
> >
> > m(isnan(m))=-1;
> > lix=sub2ind(size(m),ix{:});
> > r=m(lix);
> > disp(r);
> > %{
> > -1
> > -1
> > -1
> > %}
> >
> > us
>
>
> Ok I think i may have missed out a crucial bit of info to
> achieve what I want, so for clarity to summarise, I want to:
> 1) get the positions of non 0 values from x
> 2) use these positions to find the corresponding elements of
> b but retain their relative positions, with all other
> elements replaced by zeros.
>
> If possible, I would prefer to use the non-cells method from
> earlier in this thread. Thanks for all your help guys.
>
>

I think you're saying that where x is 0 you want b to also be 0. If
that's true, here goes:

logind = (x==0);
b(logind) = 0;

--
Loren
http://blogs.mathworks.com/loren/

Subject: find elements to include page indices

From: Peter Boettcher

Date: 24 Jul, 2008 12:36:27

Message: 17 of 17

"Dave Brackett" <davebrackett@hotmail.com> writes:

> "us " <us@neurol.unizh.ch> wrote in message
> <g69eo0$ntt$1@fred.mathworks.com>...
>> "Dave Brackett":
>> <SNIP simple question...
>>
>> > ok, and then how can I find the elements of another matrix
>> > using the indices from this result...

> Ok I think i may have missed out a crucial bit of info to
> achieve what I want, so for clarity to summarise, I want to:
> 1) get the positions of non 0 values from x
> 2) use these positions to find the corresponding elements of
> b but retain their relative positions, with all other
> elements replaced by zeros.


Yes, crucial indeed. For this to work I assume x and b are the same
size? And to restate the question: Where the elements of x are equal to
0, set the corresponding elements of b to 0.

b(~x) = 0;

Read the section of the documentation called "Logical Indexing".

-Peter

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us