g******s 发帖数: 733 | 1 次序搞不定。
d = 8 10 4
16 20 8
24 30 12
想复制成
e(:,:,1) =
8 8 8
16 16 16
24 24 24
e(:,:,2) =
10 10 10
20 20 20
30 30 30
e(:,:,3) =
4 4 4
8 8 8
12 12 12
三维数组有什么命令可以操作次序的吗?
先谢了! | r****y 发帖数: 1437 | 2
most straightforward way
for i = 1:3
e(:, :, i) = reshape(repmat(d(:, i), 1, 3), 3, 3, 1);
end
I suspect "resahpe" is not necessary. You can try it.
repmat(d(:, 1), 1, 3)
【在 g******s 的大作中提到】 : 次序搞不定。 : d = 8 10 4 : 16 20 8 : 24 30 12 : 想复制成 : e(:,:,1) = : 8 8 8 : 16 16 16 : 24 24 24 : e(:,:,2) =
| g******s 发帖数: 733 | 3 Thank you so much. You are right, reshape is not necessary. repmat is really
good :)) I am not sure if we can remove the for loop in the codes.
【在 r****y 的大作中提到】 : : : most straightforward way : : for i = 1:3 : e(:, :, i) = reshape(repmat(d(:, i), 1, 3), 3, 3, 1); : end : I suspect "resahpe" is not necessary. You can try it. : repmat(d(:, 1), 1, 3)
| g******s 发帖数: 733 | 4 I wonder why the following codes
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i = 1:3
e(:, :, i) = repmat(d(:, i), 1, 3);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
cannot be simplied as
e(:,:,1:3)=repmat(d(:,1:3),1,3);
Anyone has any ideas?
Thanks a lot!
【在 g******s 的大作中提到】 : Thank you so much. You are right, reshape is not necessary. repmat is really : good :)) I am not sure if we can remove the for loop in the codes.
| r****y 发帖数: 1437 | 5
You should at least read some basic books about matlab
before babbling here.
【在 g******s 的大作中提到】 : I wonder why the following codes : %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% : for i = 1:3 : e(:, :, i) = repmat(d(:, i), 1, 3); : end : %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% : cannot be simplied as : e(:,:,1:3)=repmat(d(:,1:3),1,3); : Anyone has any ideas? : Thanks a lot!
| F******n 发帖数: 160 | 6
回答:
可以不用循环解决这个问题 - 因为MatLab是诠释性,非编译语言,优化计算速度的基本
技巧之一就是避免循环。
几个要点提示如下:
1。你得了解线性指标 (linear indexing) 的概念:查看MaLab Help --> Programming
【在 g******s 的大作中提到】 : Thank you so much. You are right, reshape is not necessary. repmat is really : good :)) I am not sure if we can remove the for loop in the codes.
|
|