From: Mike on
On May 4, 3:01 pm, Walter Roberson <rober...(a)hushmail.com> wrote:
> 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 ;-)- Hide quoted text -
>

Another one:
>> 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

>> find(d(:,:,3)>10) % set these elements to be 999.

ans =

6

>> d(find(d(:,:,3)>10))=999. % pitfall

d(:,:,1) =

8 1 6
3 5 7
4 999 2


d(:,:,2) =

9 2 7
4 6 8
5 10 3


d(:,:,3) =

10 3 8
5 7 9
6 11 4

I know why this is wrong. I am just thinking how it can be naturally
written.

Mike
From: Walter Roberson on
Mike wrote:

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

>>> find(d(:,:,3)>10) % set these elements to be 999.
>
> ans =
>
> 6
>
>>> d(find(d(:,:,3)>10))=999. % pitfall

> I know why this is wrong. I am just thinking how it can be naturally
> written.

>> d(cat(3,false(size(d)-[0 0 1]),d(:,:,3)>10)) = 999
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 999 4
From: Walter Roberson on
Walter Roberson wrote:
> Mike wrote:
>
>> Another one:
>>>> a=magic(3);b=a+1;c=a+2;
>>>> d=cat(3,a,b,c)
>
>>>> find(d(:,:,3)>10) % set these elements to be 999.
>>
>> ans =
>>
>> 6
>>
>>>> d(find(d(:,:,3)>10))=999. % pitfall
>
>> I know why this is wrong. I am just thinking how it can be naturally
>> written.

> >> d(cat(3,false(size(d)-[0 0 1]),d(:,:,3)>10)) = 999

d(2*size(d,1)*size(d,2)+find(d(:,:,3)>10)) = 999