我试图将python程序转换成matlab,发现转置函数不能在matlab中实现。在python中,它是第一张图片。但在Matlab中,我发现它是逐行读取的,这与python非常不同。我在Matlab中使用cat和same函数,我无法找到一种方法来实现与python相同的功能。在这里输入图像描述
例如,我在matlab中创建了一个矩阵,并对其进行了整形。但我发现它不是成排阅读的。
a = [1:1:100];
b = reshape(a,2,5,10);我希望它能按行而不是列来划分。python的代码是
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的结果。
发布于 2022-08-03 09:01:40
要具有相同的形状(即相同的索引元组将访问相同的元素),您应该使用:
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)https://stackoverflow.com/questions/73216211
复制相似问题