[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Creating 3D matrices
From: |
Paul Kienzle |
Subject: |
Re: Creating 3D matrices |
Date: |
Thu, 17 Nov 2005 21:46:12 -0500 |
On Nov 17, 2005, at 8:43 PM, Colin Ingram wrote:
I've been having some problems working with 3D matrices. I'll start
with the easy stuff.
Is there a better way to make a matrix of indices in the third
dimension
than this?
X = 50;
Y = 80;
Z = 100;
matrix = zeroes(X,Y,Z);
for i = [1:Z]
matrix(:,:,i)=i*ones(X,Y);
endfor
octave> x=2; y=4; z=3;
octave> reshape(ones(x*y,1)*[1:z],[x,y,z])
ans =
ans(:,:,1) =
1 1 1 1
1 1 1 1
ans(:,:,2) =
2 2 2 2
2 2 2 2
ans(:,:,3) =
3 3 3 3
3 3 3 3
Using the function this is easier to understand but much slower:
reshape(repmat([1:z],[x*y,1]),[x,y,z])
on a similar note is there a better to build this matrix
X = 50;
Y = 80;
Z = 100;
randmat = rand(X,Y);
matrix = zeroes(X,Y,Z);
for i = [1,Z]
matrix(:,:,i)=randmat;
endfor
octave:73> A=rand(x,y); reshape(A(:)*ones(1,z),[x,y,z])
ans =
ans(:,:,1) =
0.674011 0.971078 0.710510 0.609589
0.889983 0.012567 0.990919 0.704719
ans(:,:,2) =
0.674011 0.971078 0.710510 0.609589
0.889983 0.012567 0.990919 0.704719
ans(:,:,3) =
0.674011 0.971078 0.710510 0.609589
0.889983 0.012567 0.990919 0.704719
octave:74> A
A =
0.674011 0.971078 0.710510 0.609589
0.889983 0.012567 0.990919 0.704719
Using repmat this is easier to understand but much slower:
repmat(rand(x,y),[1,1,z])
- Paul
-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.
Octave's home on the web: http://www.octave.org
How to fund new projects: http://www.octave.org/funding.html
Subscription information: http://www.octave.org/archive.html
-------------------------------------------------------------