Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

4D matrix calculation

8 views
Skip to first unread message

Francesco

unread,
Dec 20, 2011, 2:19:08 AM12/20/11
to
I have a 5D matrix Cij(3,3,Nx,Ny,Nz) where Nx,Ny and Nz are given as input.

I need to perform this:

for ikx=1:Nx,
for iky=1:Ny,
for ikz=1:Nz,

%Random simulation of fourier components
n=zeros((3,1),'double');
for j=1:3,
ncomponent=randn(2);
n(j)=complex(ncomponent(1),ncomponent(2));
%Calculation of H
H(:,ikx,iky,ikz)=dot(Cij(:,:,ikx,iky,ikz),n);
end;
end;
end;
end;

The problem is that increasing Nx,Ny,Nz the loop requires a real huge time to get the H matrix calculated.

Does anybody know any faster way to get H matrix,for example using reshape function?

Francesco

unread,
Dec 20, 2011, 5:24:08 AM12/20/11
to
"Francesco " <fra0...@hotmail.it> wrote in message <jcpctc$vv$1...@newscl01ah.mathworks.com>...
Is there nobody having a clue on how to get this 4D MATRIX?

What I mean is = H(3,Nx,Ny,Nz)= dot(Cij(3,3,Nx,Ny,Nz),n);

where n is a proper matrix made up of random numbers with gaussian distribution (mu=0,sigma=1).

Please help me.

Matt J

unread,
Dec 20, 2011, 10:17:08 AM12/20/11
to
"Francesco " <fra0...@hotmail.it> wrote in message <jcpno8$258$1...@newscl01ah.mathworks.com>...
> "Francesco " <fra0...@hotmail.it> wrote in message <jcpctc$vv$1...@newscl01ah.mathworks.com>...
> > I have a 5D matrix Cij(3,3,Nx,Ny,Nz) where Nx,Ny and Nz are given as input.
> >
> > I need to perform this:
> >
> > for ikx=1:Nx,
> > for iky=1:Ny,
> > for ikz=1:Nz,
> >
> > %Random simulation of fourier components
> > n=zeros((3,1),'double');
> > for j=1:3,
> > ncomponent=randn(2);
> > n(j)=complex(ncomponent(1),ncomponent(2));
> > %Calculation of H
> > H(:,ikx,iky,ikz)=dot(Cij(:,:,ikx,iky,ikz),n);
> > end;
> > end;
> > end;
> > end;
> >
> > The problem is that increasing Nx,Ny,Nz the loop requires a real huge time to get the H matrix calculated.
> >
> > Does anybody know any faster way to get H matrix,for example using reshape function?

It's hard to interpret what you want because you repeatedly don't show us actual executable MATLAB code with actual MATLAB syntax. This expression from your code, for example, returns an error

>> n=zeros((3,1),'double');
n=zeros((3,1),'double');
|
Error: Expression or statement is incorrect--possibly unbalanced (, {, or [.


This next expression makes no sense because you are dot multiplying a 3x3 matrix with a 3x1 vector. It would return an error.

H(:,ikx,iky,ikz)=dot(Cij(:,:,ikx,iky,ikz),n);

This 3rd expression represents the dot product of a scalar with a 3x1 vector and would also return an error


H(3,Nx,Ny,Nz)= dot(Cij(3,3,Nx,Ny,Nz),n);

Nevertheless, if I had to guess, I think you want this

H=n.'*reshape(Cij,3,[]);
H=reshape(H,[],Nx,Ny,Nz);

Francesco

unread,
Dec 21, 2011, 2:00:09 AM12/21/11
to
Hi Matt,

thanks for your reply: I will try to explain you better what I mean.

I have a 5D matrix Cij(3,3,Nx,Ny,Nz) and I need to get the final 4D matrix H(3,Nx,Ny,Nz) given by the dot product of Cij with a matrix n (at the moment I do not really know its right dimension),filled of random numbers with gaussian distribution (mu=0,sigma=1).

Afterwards, a fftn has to be accomplished.

I hope this help you to get what I would like to do.

Kind regards,
Francesco

Matt J

unread,
Dec 21, 2011, 10:23:08 AM12/21/11
to
"Francesco " <fra0...@hotmail.it> wrote in message <jcs05p$n6o$1...@newscl01ah.mathworks.com>...
>
> I hope this help you to get what I would like to do.
===========

No, it's still very fuzzy, but this tool might be what you're looking for,


http://www.mathworks.com/matlabcentral/fileexchange/25977-mtimesx-fast-matrix-multiply-with-multi-dimensional-support

Below is an example of what I think you want,


Nx=2;Ny=5;Nz=7;


Cij=rand(3,3,Nx,Ny,Nz);
n=rand(3,1);

H=mtimesx(Cij,n);
H=reshape(H,[],Nx,Ny,Nz);

>>Whos Cij H

Name Size Kilobytes Class Attributes

Cij 3x3x2x5x7 4.922 double
H 3x2x5x7 1.641 double

Gary

unread,
Dec 21, 2011, 8:26:08 PM12/21/11
to
"Francesco " <fra0...@hotmail.it> wrote in message <jcpctc$vv$1...@newscl01ah.mathworks.com>...

Gary

unread,
Dec 21, 2011, 8:28:09 PM12/21/11
to
"Francesco " <fra0...@hotmail.it> wrote in message <jcpctc$vv$1...@newscl01ah.mathworks.com>...

Gary

unread,
Dec 21, 2011, 8:29:09 PM12/21/11
to
"Francesco " <fra0...@hotmail.it> wrote in message <jcpctc$vv$1...@newscl01ah.mathworks.com>...

Gary

unread,
Dec 21, 2011, 8:30:11 PM12/21/11
to
"Matt J" wrote in message <jcstks$mms$1...@newscl01ah.mathworks.com>...
Hi Matt,

Can you help me with improving the performance for my Matlab code. I am trying to fetch stored data from a NxNx300 cell array one at a time to perform a multiplication operation. I have three big matrices. T1=T2=T3=NxNx300 sample points. So, I would like to perform T=T1*T2*T3 by fetching one value each from T1, T2 and T3.

sampleSize=300;
cell_size=676;
trans_matrix_size=2*cell_size;

nx1=nx2=0;
ny1=ny2=ny3=ny4=0;

for x=1:1:sampleSize
for mrow=1:1:trans_matrix_size
if(mrow <= cell_size)
nx1=nx1+1;
else
nx2=nx2+1;
end
for mcol=1:1:trans_matrix_size
if(mcol <= cell_size && mrow <= cell_size)
ny1=ny1+1;
ny3=0;
mat_A(mrow,mcol)=TA{nx1,ny1}(x);
mat_B(mrow,mcol)=TA{nx1,ny1}(x);
mat_C(mrow,mcol)=TA{nx1,ny1}(x);
elseif(mcol <= cell_size && mrow > cell_size) % Element a21
ny2=ny2+1;
ny4=0;
mat_A(mrow,mcol)=TC{nx2,ny2}(x);
mat_B(mrow,mcol)=Y{nx2,ny2}(x);
mat_C(mrow,mcol)=TC{nx2,ny2}(x);
elseif(mcol > cell_size && mrow <= cell_size)
ny3=ny3+1;
ny1=0;
mat_A(mrow,mcol)=TB{nx1,ny3}(x);
mat_B(mrow,mcol)=TC{nx1,ny3}(x);
mat_C(mrow,mcol)=TB{nx1,ny3}(x);
elseif(mcol > cell_size && mrow > cell_size)
ny4=ny4+1;
ny2=0;
mat_A(mrow,mcol)=TA{nx2,ny4}(x);
mat_B(mrow,mcol)=TA{nx2,ny4}(x);
mat_C(mrow,mcol)=TA{nx2,ny4}(x);
end
end
end
end

Thanks for helping out...

Matt J

unread,
Dec 22, 2011, 3:53:08 AM12/22/11
to
"Gary " <un4get...@hotmail.com> wrote in message <jcu173$k60$1...@newscl01ah.mathworks.com>...
> "Matt J" wrote in message <jcstks$mms$1...@newscl01ah.mathworks.com>...
> > "Francesco " <fra0...@hotmail.it> wrote in message <jcs05p$n6o$1...@newscl01ah.mathworks.com>...
> > >
> > > I hope this help you to get what I would like to do.
> > ===========
> >
> > No, it's still very fuzzy, but this tool might be what you're looking for,
> >
> >
> > http://www.mathworks.com/matlabcentral/fileexchange/25977-mtimesx-fast-matrix-multiply-with-multi-dimensional-support
> >
> > Below is an example of what I think you want,
> >
> >
> > Nx=2;Ny=5;Nz=7;
> >
> >
> > Cij=rand(3,3,Nx,Ny,Nz);
> > n=rand(3,1);
> >
> > H=mtimesx(Cij,n);
> > H=reshape(H,[],Nx,Ny,Nz);
> >
> > >>Whos Cij H
> >
> > Name Size Kilobytes Class Attributes
> >
> > Cij 3x3x2x5x7 4.922 double
> > H 3x2x5x7 1.641 double
>
>
>
>
> Hi Matt,
>
> Can you help me with improving the performance for my Matlab code. I am trying to fetch stored data from a NxNx300 cell array one at a time to perform a multiplication operation. I have three big matrices. T1=T2=T3=NxNx300 sample points. So, I would like to perform T=T1*T2*T3 by fetching one value each from T1, T2 and T3.
=============

Why not just convert to a normal array and then multiply as usual,

T1=cell2mat(T1);
T2=cell2mat(T2);
T3=cell2mat(T3);
T=T1.*T2.*T3;

In fact, do you have a good reason for having the T's in cell array form in the first place?

Gary

unread,
Dec 22, 2011, 4:45:10 PM12/22/11
to
"Matt J" wrote in message <jcur5k$4iq$1...@newscl01ah.mathworks.com>...
Matt,

I thought about it a little bit more last night and in fact what you have suggested is what I have implemented. I removed the cell array. I now have a 2D array matrix.
There is no reason for structuring my T's in a cell array form. Thanks for your help.
0 new messages