How do i create two multi-dimensional arrays: a 10 x 10 x 10 numerical array (3-D) anda 5 x 5 x 5 x 5 numerical array (4-D) where each value in each array corresponds to the multiplication of indices?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Yeap Jia Wei
el 5 de Mayo de 2015
Respondida: Mohmmadsoaib Diwan
el 7 de Mayo de 2015
Pls advice
0 comentarios
Respuesta aceptada
Nobel Mondal
el 5 de Mayo de 2015
Editada: Nobel Mondal
el 5 de Mayo de 2015
This could be another approach:
A = ones(10, 10, 10);
for i=1:10
A(i,:,:) = A(i,:,:)*i;
A(:,i,:) = A(:,i,:)*i;
A(:,:,i) = A(:,:,i)*i;
end
0 comentarios
Más respuestas (5)
Stephen23
el 5 de Mayo de 2015
Editada: Stephen23
el 5 de Mayo de 2015
3D
>> X = 1:10;
>> Y = X(:)*X;
>> bsxfun(@times,Y,reshape(X,1,1,10))
ans(:,:,1) =
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
...
ans(:,:,10) =
10 20 30 40 50 60 70 80 90 100
20 40 60 80 100 120 140 160 180 200
30 60 90 120 150 180 210 240 270 300
40 80 120 160 200 240 280 320 360 400
50 100 150 200 250 300 350 400 450 500
60 120 180 240 300 360 420 480 540 600
70 140 210 280 350 420 490 560 630 700
80 160 240 320 400 480 560 640 720 800
90 180 270 360 450 540 630 720 810 900
100 200 300 400 500 600 700 800 900 1000
4D
>> X = 1:5;
>> Y = X(:)*X;
>> bsxfun(@times,Y,reshape(Y,1,1,5,5))
ans(:,:,1,1) =
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
...
ans(:,:,5,5) =
25 50 75 100 125
50 100 150 200 250
75 150 225 300 375
100 200 300 400 500
125 250 375 500 625
0 comentarios
Mohmmadsoaib Diwan
el 7 de Mayo de 2015
array_3D=zeros(10,10,10);
if true
% code
endfor x=(1:10)
for y=(1:10);
for z=(1:10);
array_3D(x,y,z)=(x*y*z);
end
end
end
array_4D=zeros(5,5,5,5);
for x=(1:5)
for y=(1:5)
for z=(1:5)
for t=(1:5)
array_4D(x,y,z,t)=(x*y*z*t);
end
end
end
end
0 comentarios
Charlie Elverson
el 5 de Mayo de 2015
I think something like this would work:
my_array = zeros(10, 10, 10)
for i = 1:10
for j = 1:10
for k = 1:10
my_array(i,j,k) = i*j*k;
end
end
end
This could obviously be made more general, but you get the idea. The 4-dimensional case is a simple extension of the above.
Andrei Bobrov
el 5 de Mayo de 2015
Editada: Andrei Bobrov
el 5 de Mayo de 2015
variant
3D:
n = [10 10 10];
A = bsxfun(@times,(1:n(1))'*(1:n(2)),reshape(1:n(3),1,1,[]));
4D:
n = [5 5 5 5];
A = bsxfun(@times(bsxfun(@times,(1:n(1))'*(1:n(2)),...
reshape(1:n(3),1,1,[])),reshape(1:n(4),1,1,1,[]));
other variant:
n=[4 5 2 3];
nn = numel(n);
z = cell(nn,1);
ii = arrayfun(@(x)1:x,n,'un',0);
[z{:}] = ndgrid(ii{:});
out = prod(cat(nn+1,z{:}),nn+1);
0 comentarios
Ver también
Categorías
Más información sobre Operators and Elementary Operations en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!