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:
Using all or find function?

Subject: Using all or find function?

From: Lin

Date: 16 Mar, 2009 11:08:12

Message: 1 of 6

Hi all, say I have,

T1 = {0 11 12 0 0 0 0 0 0 0 0;
      0 0 0 0 0 11 0 0 0 0 0;
      0 0 0 0 0 12 0 0 0 0 0;
      0 0 0 0 0 0 0 0 0 0 0;
      0 0 0 0 0 0 0 0 0 0 0};
 
T2 = {0 21 0 22 0 0 0 0 0 0 0;
      0 0 0 0 0 0 21 0 0 0 0;
      0 0 0 0 0 0 0 0 0 0 0;
      0 0 0 0 0 0 22 0 0 0 0;
      0 0 0 0 0 0 0 0 0 0 0};

 S{1} = sparse(cell2mat(T1));
 S{2} = sparse(cell2mat(T2));

Suppose, I need to find all the '11' from S{1} and perform some other operations (say disp). I tried using all and find function, but i cannot display the correct result. Could someone suggest which function best suit my situation and point out where i made a mistake? I have attached my codes. Thanks for helping...

%Using all function
if (all(S{1}==11))
%perform some other operation say, disp
disp('content found')
end;

%Using find function
if (find(S{1}==11))
%perform some operation
disp('content found')
end;

Subject: Using all or find function?

From: Bruno Luong

Date: 16 Mar, 2009 11:33:12

Message: 2 of 6

Have you actually read the doc of FIND and ALL? You need to *understand* what they return.

Bruno

Subject: Using all or find function?

From: Lin

Date: 16 Mar, 2009 15:13:14

Message: 3 of 6

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <gplddo$sgp$1@fred.mathworks.com>...
> Have you actually read the doc of FIND and ALL? You need to *understand* what they return.
>
> Bruno

Here is what i got. Do correct me if i am wrong.

%find
%store the indices
[i,j,k] = find(S{1}==11)

%all
return a '1' if all elements are non-zero and '0' if one or more is zero.

It means we cant use the all function in this context.

I thought of using a nested for-loop to go through S{1}, but it is a rather inefficient way. I am not sure if I can make use of the find function to perform the task, since we are able to locate the two '11' from S{1}. Any ideas? Thanks.

Subject: Using all or find function?

From: us

Date: 16 Mar, 2009 15:30:17

Message: 4 of 6

"Lin" <shenqlv@yahoo.com> wrote in message <gplbur$pik$1@fred.mathworks.com>...
> Suppose, I need to find all the '11' from S{1} and perform some other operations (say disp). I tried using all and find function, but i cannot display the correct result. Could someone suggest which function best suit my situation and point out where i made a mistake? I have attached my codes. Thanks for helping...
> %Using all function
> if (all(S{1}==11))
> %perform some other operation say, disp
> disp('content found')
> end;
> %Using find function
> if (find(S{1}==11))
> %perform some operation
> disp('content found')
> end;

this is a bit confusing...
why not use ISEMPTY in combination with FIND, eg

     clear s; % <- save old stuff!
     s{1}=sparse([0,11,11;11,0,0;0,11,0]);
     [sr,sc]=find(s{1}==11); % <- get row#/col#
if ~isempty(sr)
     disp(sprintf('# instances = %-1d',numel(sr)));
     disp('at:');
     disp([sr,sc]);
end
%{
# instances = 4
at row col:
     2 1
     1 2
     3 2
     1 3
%}

just a thought...
us

Subject: Using all or find function?

From: Lin

Date: 16 Mar, 2009 16:26:11

Message: 5 of 6

"us " <us@neurol.unizh.ch> wrote in message <gplra9$6c7$1@fred.mathworks.com>...

> this is a bit confusing...
> why not use ISEMPTY in combination with FIND, eg
>
> clear s; % <- save old stuff!
> s{1}=sparse([0,11,11;11,0,0;0,11,0]);
> [sr,sc]=find(s{1}==11); % <- get row#/col#
> if ~isempty(sr)
> disp(sprintf('# instances = %-1d',numel(sr)));
> disp('at:');
> disp([sr,sc]);
> end

> just a thought...
> us

Thanks for replying, I would like to apologise for the confusion, but this is not what i want. Let me give a more specific example. Say

x = [0 11 12; 0 12 11; 0 12 12];
for row=1:3 %nested for-loop
for col=1:3

if (x(row,col) == 11)
disp('11 is found')
end

end
end

%result
11 is found
11 is found

Now extend to something as follows:

T1 = {0 11 12 0 0 0 0 0 0 0 0;
      0 0 0 0 0 11 0 0 0 0 0;
      0 0 0 0 0 12 0 0 0 0 0;
      0 0 0 0 0 0 0 0 0 0 0;
      0 0 0 0 0 0 0 0 0 0 0};
 
T2 = {0 21 0 22 0 0 0 0 0 0 0;
      0 0 0 0 0 0 21 0 0 0 0;
      0 0 0 0 0 0 0 0 0 0 0;
      0 0 0 0 0 0 22 0 0 0 0;
      0 0 0 0 0 0 0 0 0 0 0};

 S{1} = sparse(cell2mat(T1));
 S{2} = sparse(cell2mat(T2));

% Suppose to be nested-for loop as before.. However, I cant get the same results using the nested for-loop. Is there something else which can achieve what i need?

#results
11 is found
11 is found

Thanks.. Hope this is clear enough.

Subject: Using all or find function?

From: us

Date: 16 Mar, 2009 16:44:12

Message: 6 of 6

"Lin"
> Thanks for replying, I would like to apologise for the confusion, but this is not what i want. Let me give a more specific example. Say
> x = [0 11 12; 0 12 11; 0 12 12];
> for row=1:3 %nested for-loop
> for col=1:3
> if (x(row,col) == 11)
> disp('11 is found')
> end
> end
> end

> S{1} = sparse(cell2mat(T1));
> S{2} = sparse(cell2mat(T2));
> % Suppose to be nested-for loop as before.. However, I cant get the same results using the nested for-loop. Is there something else which can achieve what i need> Thanks.. Hope this is clear enough...

well, why did you not mention the nested for-loop before...
moreover, the proposed snippet does exactly what you want - did you bother to look at it?...

again,

     clear s; % <- save old stuff!
     s{1}=sparse([0,11,11;11,0,0;0,11,0]);
     [sr,sc]=find(s{1}==11); % <- get row#/col#
% now, SR/SC contain the valid row/col numbers, which you
% extract in your nested loop in
% if (x(row,col) == 11)
% hence, if you really want to do things in a loop, use it as follows

for i=1:numel(sr)
     disp(sprintf('11 found at row/col# %3d/%3d',sr(i),sc(i)));
end
%{
11 found at row/col# 2/ 1
11 found at row/col# 1/ 2
11 found at row/col# 3/ 2
11 found at row/col# 1/ 3
%}

us

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