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?
2 次查看(过去 30 天)
显示 更早的评论
Pls advice
0 个评论
采纳的回答
Nobel Mondal
2015-5-5
编辑:Nobel Mondal
2015-5-5
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 个评论
更多回答(5 个)
Stephen23
2015-5-5
编辑:Stephen23
2015-5-5
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 个评论
Mohmmadsoaib Diwan
2015-5-7
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 个评论
Charlie Elverson
2015-5-5
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.
2 个评论
Andrei Bobrov
2015-5-5
编辑:Andrei Bobrov
2015-5-5
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Operators and Elementary Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!