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:
Searching content in multi-dimensional matrix

Subject: Searching content in multi-dimensional matrix

From: Lin

Date: 13 Mar, 2009 05:09:01

Message: 1 of 5

Hi all.

Say,
A= cat (3, { [1;0], [], []
                  [], [], [1;0]
                  [], [], [] }, { [], [], [1;0]
                                        [], [], []
                                        [], [], [1;0] } );


I need to perform 2 tasks.
1) Search through A to see if there is a [1;0] matrix
2) Stored those indices.

I used a nested for-loop to perform the 1st task.

for page = 1:2
for row= 1:3
for column = 1:3

if (isequal(A{row,column,page}, [1;0]))
disp ('[1;0] is found in A')

end
end
end
end

for 2nd task,

I tried using
[i, j,k] = find(A==[1;0])

but there is an error. Is there an efficient way to perform both tasks? Thanks for helping.

Subject: Searching content in multi-dimensional matrix

From: Bruno Luong

Date: 13 Mar, 2009 06:04:09

Message: 2 of 5

"Lin " <shenqlv@yahoo.com> wrote in message <gpcppd$a5n$1@fred.mathworks.com>...
> Hi all.
>
> Say,
> A= cat (3, { [1;0], [], []
> [], [], [1;0]
> [], [], [] }, { [], [], [1;0]
> [], [], []
> [], [], [1;0] } );
>
>
> I need to perform 2 tasks.
> 1) Search through A to see if there is a [1;0] matrix
> 2) Stored those indices.
>
> I used a nested for-loop to perform the 1st task.
>
> for page = 1:2
> for row= 1:3
> for column = 1:3
>
> if (isequal(A{row,column,page}, [1;0]))
> disp ('[1;0] is found in A')
>
> end
> end
> end
> end
>
> for 2nd task,
>
> I tried using
> [i, j,k] = find(A==[1;0])
>
> but there is an error. Is there an efficient way to perform both tasks? Thanks for helping.

You could do this:

A= cat (3, { [1;0] [] [];
             [] [] [1;0];
             [] [] [] }, ...
           { [] [] [1;0];
             [] [] [];
             [] [] [1;0] } );
find10 = @(x) numel(x)==2 && all(x(1:2)==[1; 0]);
ilin=find(cellfun(find10,A));
[i j k]=ind2sub(size(A),ilin)

But using cell is rarely suitable for any efficiency during manipulation. If it's not too late, I recommend using arrays (full or sparse). You might need 2 or 3 arrays to store the same thing, but it will be more efficient to work with hem.

Bruno

Subject: Searching content in multi-dimensional matrix

From: Lin

Date: 13 Mar, 2009 08:57:01

Message: 3 of 5

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <gpct0p$3gc$1@fred.mathworks.com>...
> "Lin " <shenqlv@yahoo.com> wrote in message <gpcppd$a5n$1@fred.mathworks.com>...
>
> You could do this:
>
> A= cat (3, { [1;0] [] [];
> [] [] [1;0];
> [] [] [] }, ...
> { [] [] [1;0];
> [] [] [];
> [] [] [1;0] } );
> find10 = @(x) numel(x)==2 && all(x(1:2)==[1; 0]);
> ilin=find(cellfun(find10,A));
> [i j k]=ind2sub(size(A),ilin)
>
> But using cell is rarely suitable for any efficiency during manipulation. If it's not too late, I recommend using arrays (full or sparse). You might need 2 or 3 arrays to store the same thing, but it will be more efficient to work with hem.
>
> Bruno

Hi Bruno.. Thanks. the code works. However, can do you a little explanation on this sentence
find10 = @(x) numel(x)==2 && all(x(1:2)==[1; 0]);

Do correct me if i am wrong. this sentence creates a function handle. @x will read in the value and pass it into numel (x) where it returns the number of elements based on the conditions you have set? do provide me a little explanation.. thanks..

Subject: Searching content in multi-dimensional matrix

From: Bruno Luong

Date: 13 Mar, 2009 09:32:01

Message: 4 of 5

"Lin " <shenqlv@yahoo.com> wrote in message <gpd74t$2i4$1@fred.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <gpct0p$3gc$1@fred.mathworks.com>...
> > "Lin " <shenqlv@yahoo.com> wrote in message <gpcppd$a5n$1@fred.mathworks.com>...
> >
> > You could do this:
> >
> > A= cat (3, { [1;0] [] [];
> > [] [] [1;0];
> > [] [] [] }, ...
> > { [] [] [1;0];
> > [] [] [];
> > [] [] [1;0] } );
> > find10 = @(x) numel(x)==2 && all(x(1:2)==[1; 0]);
> > ilin=find(cellfun(find10,A));
> > [i j k]=ind2sub(size(A),ilin)
> >
> > But using cell is rarely suitable for any efficiency during manipulation. If it's not too late, I recommend using arrays (full or sparse). You might need 2 or 3 arrays to store the same thing, but it will be more efficient to work with hem.
> >
> > Bruno
>
> Hi Bruno.. Thanks. the code works. However, can do you a little explanation on this sentence
> find10 = @(x) numel(x)==2 && all(x(1:2)==[1; 0]);
>

This function takes as input x (x is an element of the cell array A) and do the following check
1. x must have 2 elements (numel(x)==2), if succesful
2. compare x with [1; 0]: (all(x(1:2)==[1; 0]))

It is important to use the sortcut AND "&&" so that the second test is not carried out when numel(x) is different 2 (for example when x is empty).

Bruno

Subject: Searching content in multi-dimensional matrix

From: Steven Lord

Date: 14 Mar, 2009 02:05:51

Message: 5 of 5


"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message
news:gpct0p$3gc$1@fred.mathworks.com...
> "Lin " <shenqlv@yahoo.com> wrote in message
> <gpcppd$a5n$1@fred.mathworks.com>...

*snip*

> You could do this:
>
> A= cat (3, { [1;0] [] [];
> [] [] [1;0];
> [] [] [] }, ...
> { [] [] [1;0];
> [] [] [];
> [] [] [1;0] } );
> find10 = @(x) numel(x)==2 && all(x(1:2)==[1; 0]);
> ilin=find(cellfun(find10,A));

You don't need to check numel if you use ISEQUAL.

find10 = @(x) isequal(x, [1; 0]);
ilin = find(cellfun(find10, A));

In fact, your find10 errors out if you pass in [1, 0] (which passes the
NUMEL test but errors when you try to compare it with [1; 0].)

--
Steve Lord
slord@mathworks.com

Tags for this Thread

No tags are associated with 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