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:
Average among matrices

Subject: Average among matrices

From: Filoche

Date: 23 Sep, 2010 14:54:37

Message: 1 of 9

Hi everyone.

Let's say I have 1 struct with 3 entries. And in those entries, I have a X matrix.

So :

mystruct(1).X
mystruct(2).X
mystruct(3).X

Is there an easy to get an average matrix of all those X matrix? I mean beside looping each element in the struct.

Also, is there a way to retrieve all the X matrix at once? To get a 3d matrix like X(3,rows,cols)


With regards,
Phil

Subject: Average among matrices

From: Matt J

Date: 23 Sep, 2010 15:15:24

Message: 2 of 9

Filoche <pmassicotte@hotmail.com> wrote in message <1091046373.102240.1285253707865.JavaMail.root@gallium.mathforum.org>...
> Hi everyone.
>
> Let's say I have 1 struct with 3 entries. And in those entries, I have a X matrix.
>
> So :
>
> mystruct(1).X
> mystruct(2).X
> mystruct(3).X
>
> Is there an easy to get an average matrix of all those X matrix? I mean beside looping each element in the struct.
>
> Also, is there a way to retrieve all the X matrix at once? To get a 3d matrix like X(3,rows,cols)

Here's approximately what you asked for. It's normally better to have the 3D matrix be rows x cols x 3 if you're going to be retrieving individual X from this (more memory locality).

X3d=cat(3,mystruct.X);
Xmean=mean(X3d,3);

Subject: Average among matrices

From: Filoche

Date: 23 Sep, 2010 18:31:16

Message: 3 of 9

Thank you Matt for this answer. This is working perfectly.

One more quick question if you guys dont mind.

Let's say I have 2 vectors (u and v).

I would like to create a matrix from them that will contain

u(1) + v(1), u(1) + v(2), ...+... , u(1) + v(n)
u(2) + v(1), u(1) + v(2), ...+... , u(2) + v(n)
..
u(n) + v(1), u(1) + v(2), ...+... , u(2) + v(n)

I'm still trying to avoid using loop (for speed optimization). Anyone got an idea of I vectorize this?

With regards,
Phil

Subject: Average among matrices

From: Matt J

Date: 23 Sep, 2010 19:20:21

Message: 4 of 9

Filoche <pmassicotte@hotmail.com> wrote in message <1652843549.103490.1285266706934.JavaMail.root@gallium.mathforum.org>...

> I would like to create a matrix from them that will contain
>
> u(1) + v(1), u(1) + v(2), ...+... , u(1) + v(n)
> u(2) + v(1), u(1) + v(2), ...+... , u(2) + v(n)
> ..
> u(n) + v(1), u(1) + v(2), ...+... , u(2) + v(n)
>
> I'm still trying to avoid using loop (for speed optimization). Anyone got an idea of I vectorize this?
=========


bsxfun(@plus,u(:),v(:).');

Subject: Average among matrices

From: Filoche

Date: 23 Sep, 2010 19:34:53

Message: 5 of 9

Once again, thank you sir for you help.

Much appreciated.

With regards,
Phil

Subject: Average among matrices

From: Filoche

Date: 24 Sep, 2010 17:18:57

Message: 6 of 9

Hi again everyone.

I'm still working with matrices and I have 1 more question.

Suppose I have a matrix M of 181 x 45 elements.

I want to add a slit of size of 20 in a specific diagonal. However, I cant use matrix index to find the diagonal since X and Y axis differs.

My X and Y axis are
x = 230:5:450;
y = 240:2:600;

I want to add a slit (of width = 20) on the diagonal x = y.

For instance, I was using the following to find index of this.

[~, xindex, yindex] = interesct(x,y);

But I'm lost after that.

Any help would be greatly appreciated.

With regards,
Phil

Subject: Average among matrices

From: Matt J

Date: 24 Sep, 2010 18:47:04

Message: 7 of 9

Filoche <pmassicotte@hotmail.com> wrote in message <1408324225.109169.1285348768613.JavaMail.root@gallium.mathforum.org>...
> Hi again everyone.
>
> I'm still working with matrices and I have 1 more question.
>
> Suppose I have a matrix M of 181 x 45 elements.
>
> I want to add a slit of size of 20 in a specific diagonal. However, I cant use matrix index to find the diagonal since X and Y axis differs.
>
> My X and Y axis are
> x = 230:5:450;
> y = 240:2:600;
>
> I want to add a slit (of width = 20) on the diagonal x = y.
>
> For instance, I was using the following to find index of this.
>
> [~, xindex, yindex] = interesct(x,y);
>

You haven't explained what a "slit" is, but I'm guessing want something like

idx=sub2ind(size(M), xindex,yindex);
M(idx)=slitValue;

Subject: Average among matrices

From: Filoche

Date: 24 Sep, 2010 19:11:11

Message: 8 of 9

Hi there.

When I say a slit I mean a band of a specific width (20 in this case).

I'm trying your solution right now.

Regards,
Phil

Subject: Average among matrices

From: Filoche

Date: 24 Sep, 2010 19:23:26

Message: 9 of 9

Hi again.

This is almost exactly what I need. For instance it will change the value on the diagonal of width = 1. Would it be more complex to specify a diagonal width of let's say 10. In other words, 5 points up the diagonal and 5 points down the diagonal.

What I'm trying to do, is to remove some fluorescence scattering in my data which occurs at x = y. But this scattering occurs in a somewhat larger area than at x = y.

With regards,
Phil

Tags for this Thread

No tags are associated with 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