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 function

Subject: find function

From: Wanderson

Date: 2 Jul, 2013 10:40:10

Message: 1 of 6

Hi, I was looking for the past threat about find function but I didn't found what I want.

For example, I have a matrix 'A' bellow and I want to know all the positions where the value of 'A' is smaller than 3.

A = [1 2 3; 3 4 2; 2 3 4];

[rows,col,val] = find(A<3);

Now, I need to create a new matrix called SOL, the same size as A and within the position where A<3 I need to store the value 1;

I thought that this following code could work, but didn't work.

SOL = zeros(3);
SOL(rows,col) = 1;


Thanks for the help.

Subject: find function

From: Torsten

Date: 2 Jul, 2013 11:14:10

Message: 2 of 6

"Wanderson " <wanderson.ferreira@usp.br> wrote in message <kquama$1tf$1@newscl01ah.mathworks.com>...
> Hi, I was looking for the past threat about find function but I didn't found what I want.
>
> For example, I have a matrix 'A' bellow and I want to know all the positions where the value of 'A' is smaller than 3.
>
> A = [1 2 3; 3 4 2; 2 3 4];
>
> [rows,col,val] = find(A<3);
>
> Now, I need to create a new matrix called SOL, the same size as A and within the position where A<3 I need to store the value 1;
>
> I thought that this following code could work, but didn't work.
>
> SOL = zeros(3);
> SOL(rows,col) = 1;
>
>
> Thanks for the help.

Try
SOL=zeros(3,3);

Best wishes
Torsten.

Subject: find function

From: Wanderson

Date: 2 Jul, 2013 12:24:11

Message: 3 of 6

"Torsten" wrote in message <kqucm2$63p$1@newscl01ah.mathworks.com>...
> "Wanderson " <wanderson.ferreira@usp.br> wrote in message <kquama$1tf$1@newscl01ah.mathworks.com>...
> > Hi, I was looking for the past threat about find function but I didn't found what I want.
> >
> > For example, I have a matrix 'A' bellow and I want to know all the positions where the value of 'A' is smaller than 3.
> >
> > A = [1 2 3; 3 4 2; 2 3 4];
> >
> > [rows,col,val] = find(A<3);
> >
> > Now, I need to create a new matrix called SOL, the same size as A and within the position where A<3 I need to store the value 1;
> >
> > I thought that this following code could work, but didn't work.
> >
> > SOL = zeros(3);
> > SOL(rows,col) = 1;
> >
> >
> > Thanks for the help.
>
> Try
> SOL=zeros(3,3);
>
> Best wishes
> Torsten.

LoL, I was almost sleeping when I wrote this, sorry by the bad english rsrsrs.

So, Torsten, your advice didn't work.
I already figure out how to do that. Must be better ways but..

h = find(A<3);
SOL = zeros(3);
SOL = SOL(:);
SOL(h) = 1;

SOL = repmat(SOL,3,3);

Thanx anyway.

Subject: find function

From: dpb

Date: 2 Jul, 2013 13:15:30

Message: 4 of 6

On 7/2/2013 7:24 AM, Wanderson wrote:
> "Torsten" wrote in message <kqucm2$63p$1@newscl01ah.mathworks.com>...
>> "Wanderson " <wanderson.ferreira@usp.br> wrote in message
>> <kquama$1tf$1@newscl01ah.mathworks.com>...
...

>> > > ... I have a matrix 'A' bellow and I want to know all the
>> positions where the value of 'A' is smaller than 3.
...
>> > > Now, I need to create a new matrix called SOL, the same size as A
>> and within the position where A<3 I need to store the value 1;
>> > > I thought that this following code could work, but didn't work.
>> > > SOL = zeros(3);
>> > SOL(rows,col) = 1;
>> > > > Thanks for the help.
>>
>> Try
>> SOL=zeros(3,3);
>>
...

> I already figure out how to do that. Must be better ways but..
>
> h = find(A<3);
> SOL = zeros(3);
> SOL = SOL(:);
> SOL(h) = 1;
>
> SOL = repmat(SOL,3,3);

Use the logical address array directly instead of FIND

SOL=zeros(size(A)); % to get target desired size/shape
SOL(A<3) = 1; % set the desired values

--

Subject: find function

From: Steven_Lord

Date: 2 Jul, 2013 14:04:10

Message: 5 of 6



"dpb" <none@non.net> wrote in message news:kqujo9$e63$1@speranza.aioe.org...
> On 7/2/2013 7:24 AM, Wanderson wrote:
>> "Torsten" wrote in message <kqucm2$63p$1@newscl01ah.mathworks.com>...
>>> "Wanderson " <wanderson.ferreira@usp.br> wrote in message
>>> <kquama$1tf$1@newscl01ah.mathworks.com>...
> ...
>
>>> > > ... I have a matrix 'A' bellow and I want to know all the
>>> positions where the value of 'A' is smaller than 3.
> ...
>>> > > Now, I need to create a new matrix called SOL, the same size as A
>>> and within the position where A<3 I need to store the value 1;
>>> > > I thought that this following code could work, but didn't work.
>>> > > SOL = zeros(3);
>>> > SOL(rows,col) = 1;
>>> > > > Thanks for the help.
>>>
>>> Try
>>> SOL=zeros(3,3);
>>>
> ...
>
>> I already figure out how to do that. Must be better ways but..
>>
>> h = find(A<3);
>> SOL = zeros(3);
>> SOL = SOL(:);
>> SOL(h) = 1;
>>
>> SOL = repmat(SOL,3,3);
>
> Use the logical address array directly instead of FIND
>
> SOL=zeros(size(A)); % to get target desired size/shape
> SOL(A<3) = 1; % set the desired values

Or just convert the logical array to double.

SOL = double(A < 3);

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: find function

From: dpb

Date: 2 Jul, 2013 14:23:05

Message: 6 of 6

On 7/2/2013 9:04 AM, Steven_Lord wrote:
> "dpb" <none@non.net> wrote in message
> news:kqujo9$e63$1@speranza.aioe.org...
...

>> Use the logical address array directly instead of FIND
>>
>> SOL=zeros(size(A)); % to get target desired size/shape
>> SOL(A<3) = 1; % set the desired values
>
> Or just convert the logical array to double.
>
> SOL = double(A < 3);

That works for the special case of 1 being the target value. Figured
pedagogical value of setting a particular value would be useful to OP.
Of course, could also multiply your form by a constant (all of which
goes to again show more than one way to skin the proverbial feline :) ).

--

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