From: wapo on
Hi all,

I have a 3D matrix and a 2D matrix which I will use as mask in each
slice of the matrix (third dim). For example

a=rand(3,3,10);
b=eye(3);
for i=1:10
c=a(b,i);
end

The above doesn't work but one can see what I want. From each slice,
i, in the matrix I want the diagonal elements.

Any suggestion

Thanks in advance

Apo
From: Matt on
wapo <apostolos1975(a)gmail.com> wrote in message <fcdbacaf-1839-4cfe-9f8c-effc1af11bad(a)z24g2000yqb.googlegroups.com>...
> Hi all,
>
> I have a 3D matrix and a 2D matrix which I will use as mask in each
> slice of the matrix (third dim). For example
>
> a=rand(3,3,10);
> b=eye(3);
> for i=1:10
> c=a(b,i);
> end
>
> The above doesn't work but one can see what I want. From each slice,
> i, in the matrix I want the diagonal elements.

Perhaps as follows:

aa=reshape(a,9,[]);
c=aa([1 5 9],:),
From: wapo on
On Sep 10, 12:38 pm, "Matt " <x...(a)whatever.com> wrote:
> wapo <apostolos1...(a)gmail.com> wrote in message <fcdbacaf-1839-4cfe-9f8c-effc1af11...(a)z24g2000yqb.googlegroups.com>...
> > Hi all,
>
> > I have a 3D matrix and a 2D matrix which I will use as mask in each
> > slice of the matrix (third dim). For example
>
> > a=rand(3,3,10);
> > b=eye(3);
> > for i=1:10
> >    c=a(b,i);
> > end
>
> > The above doesn't work but one can see what I want. From each slice,
> > i, in the matrix I want the diagonal elements.
>
> Perhaps as follows:
>
> aa=reshape(a,9,[]);
> c=aa([1 5 9],:),

I was hoping to avoid copying data around and read them directly from
the original matrix.
From: Matt on
"Matt " <xys(a)whatever.com> wrote in message <h8akut$61b$1(a)fred.mathworks.com>...
> wapo <apostolos1975(a)gmail.com> wrote in message <fcdbacaf-1839-4cfe-9f8c-effc1af11bad(a)z24g2000yqb.googlegroups.com>...
> > Hi all,
> >
> > I have a 3D matrix

BTW, matrices are 2D by definition :)
From: ImageAnalyst on
On Sep 10, 6:26 am, wapo <apostolos1...(a)gmail.com> wrote:
> Hi all,
>
> I have a 3D matrix and a 2D matrix which I will use as mask in each
> slice of the matrix (third dim). For example
>
> a=rand(3,3,10);
> b=eye(3);
> for i=1:10
>    c=a(b,i);
> end
>
> The above doesn't work but one can see what I want. From each slice,
> i, in the matrix I want the diagonal elements.
>
> Any suggestion
>
> Thanks in advance
>
> Apo

----------------------------------------------------------------------------------------------------------------------
This is what I'd do:

a=rand(3,3,10);
c = zeros(size(a));
b=eye(3);
for i=1:10
% Multiple each slice by the mask, pixel by pixel.
c(:,:,i) = a(:,:,i) .* b % Note the dot before the *
end

There's probably a much more compact, cryptic way of doing it, but
this is easy to understand and pretty fast unless you have hundreds of
megabytes in your array.
-ImageAnalyst
 |  Next  |  Last
Pages: 1 2
Prev: Infeasible start point
Next: BUFR files