Redian新闻
>
请问怎么把这个一维向量复制成三维数组
avatar
请问怎么把这个一维向量复制成三维数组# Computation - 科学计算
g*s
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
三维数组有什么命令可以操作次序的吗?
先谢了!
avatar
r*y
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) =

avatar
g*s
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)

avatar
g*s
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.

avatar
r*y
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!

avatar
F*n
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.

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。