首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将矩阵重塑为3D,但我发现matlab将按照列来完成这一任务

如何将矩阵重塑为3D,但我发现matlab将按照列来完成这一任务
EN

Stack Overflow用户
提问于 2022-08-03 04:12:21
回答 1查看 40关注 0票数 0

我试图将python程序转换成matlab,发现转置函数不能在matlab中实现。在python中,它是第一张图片。但在Matlab中,我发现它是逐行读取的,这与python非常不同。我在Matlab中使用cat和same函数,我无法找到一种方法来实现与python相同的功能。在这里输入图像描述

例如,我在matlab中创建了一个矩阵,并对其进行了整形。但我发现它不是成排阅读的。

代码语言:javascript
复制
a = [1:1:100];
b = reshape(a,2,5,10);

我希望它能按行而不是列来划分。python的代码是

代码语言:javascript
复制
a = np.linspace(0,10*10-1,10*10)
b = a.reshape(10,10)
c = np.transpose(b[0::2],b[1::2],axes=(1,0,2))

所以我想知道在python1:https://i.stack.imgur.com/kc4Er.jpg中是否有类似c的结果。

EN

回答 1

Stack Overflow用户

发布于 2022-08-03 09:01:40

要具有相同的形状(即相同的索引元组将访问相同的元素),您应该使用:

代码语言:javascript
复制
a = 0:1:99;  % Note that your python code goes from 0 to 99, not from 1 to 100
b = reshape(a, 10, 10);
b = b';  % This puts b in a similar order as numpy would use, so it becomes easier to use;
c = permute(cat(3, b(1:2:end,:), b(2:2:end, :)), [1, 3, 2]);  % Equivalent to numpy transpose of the concatenated array on your python code

% Note that the way Matlab displays the matrices is different, but the indices are matching:
assert(c(1,1,1) == 0)
assert(c(1,1,2) == 1)
assert(c(1,2,1) == 10)
assert(c(1,2,2) == 11)
assert(c(2,2,2) == 31)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73216211

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档