From: Mike on
hi

just a simple question.
>> a=magic(4)

a =

16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1

>> [r,c]=find(a>13)

r =

1
4
4


c =

1
2
3

Then I'd like only those data in r and c set to 255.
How to do this please?

Mike
From: Steven Lord on

"Mike" <sulfateion(a)gmail.com> wrote in message
news:b2ca8e16-9816-4f2d-ba64-3bead1381948(a)s13g2000prc.googlegroups.com...
> hi
>
> just a simple question.
>>> a=magic(4)
>
> a =
>
> 16 2 3 13
> 5 11 10 8
> 9 7 6 12
> 4 14 15 1
>
>>> [r,c]=find(a>13)

If you want to use this information to modify the locations in a, don't use
the two output form of FIND -- in fact, don't use FIND at all.

a = magic(4);
a(a > 13) = -1;

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ


From: Mike on
On May 4, 12:11 pm, "Steven Lord" <sl...(a)mathworks.com> wrote:
> "Mike" <sulfate...(a)gmail.com> wrote in message
>
> news:b2ca8e16-9816-4f2d-ba64-3bead1381948(a)s13g2000prc.googlegroups.com...
>
> > hi
>
> > just a simple question.
> >>> a=magic(4)
>
> > a =
>
> >    16     2     3    13
> >     5    11    10     8
> >     9     7     6    12
> >     4    14    15     1
>
> >>> [r,c]=find(a>13)
>
> If you want to use this information to modify the locations in a, don't use
> the two output form of FIND -- in fact, don't use FIND at all.
>
> a = magic(4);
> a(a > 13) = -1;
>
> --
> Steve Lord
> sl...(a)mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ:http://matlabwiki.mathworks.com/MATLAB_FAQ

Thank you very much. Another similar question but with more
dimension:

>> a=magic(3);b=a+1;c=a+2;
>> d=cat(3,a,b,c)

d(:,:,1) =

8 1 6
3 5 7
4 9 2


d(:,:,2) =

9 2 7
4 6 8
5 10 3


d(:,:,3) =

10 3 8
5 7 9
6 11 4

>> d(:,:,1)(d(:,:,1)>8)=999
??? Error: ()-indexing must appear last in an index expression.

>> dd=d(:,:,1);dd(dd>8)=999

dd =

8 1 6
3 5 7
4 999 2

Is there a way not to create a temporary "dd"?

Mike
From: Walter Roberson on
Mike wrote:

>>> d(:,:,1)(d(:,:,1)>8)=999
> ??? Error: ()-indexing must appear last in an index expression.
>
>>> dd=d(:,:,1);dd(dd>8)=999
>
> dd =
>
> 8 1 6
> 3 5 7
> 4 999 2
>
> Is there a way not to create a temporary "dd"?

Yup.

d(vertcat(d(:,:,1)>8, false(size(d,1)*size(d,2),1))) = 999;

Didn't say it would be short or easy to understand.
From: Walter Roberson on
Walter Roberson wrote:
> Mike wrote:
>
>>>> d(:,:,1)(d(:,:,1)>8)=999
>> ??? Error: ()-indexing must appear last in an index expression.
>>
>>>> dd=d(:,:,1);dd(dd>8)=999
>>
>> dd =
>>
>> 8 1 6
>> 3 5 7
>> 4 999 2
>>
>> Is there a way not to create a temporary "dd"?
>
> Yup.
>
> d(vertcat(d(:,:,1)>8, false(size(d,1)*size(d,2),1))) = 999;
>
> Didn't say it would be short or easy to understand.

Or error-proof either. I should have written,

d(vertcat(d(:,:,1)>8, false(prod(size(d)-[0 0 1]),1))) = 999;

Though now that I think of it,

d(find(d(:,:,1)>8)) = 999;

should work as well.

Both versions will break if fixed index being searched over does not
happen to be 1, but I suspect more people would get a hint on changing
the code from the longer version; the reason the second shorter one
works for the first dimension only is more obscure... which is probably
why I used the more general version of it in a posting a few years ago ;-)