【问题标题】:Coordinates after reshaping 4d matrix in Matlab在 Matlab 中重塑 4d 矩阵后的坐标
【发布时间】:2018-10-30 17:33:06
【问题描述】:

假设我在 Matlab 中有一个矩阵 A,大小为 MxNxKxP。假设我使用reshapeA 转换为列向量B。我想要一个代码,给定A(i,j,h,p)th 元素的坐标,给我B 中相同元素的坐标k。你能帮我做吗?

This 代码可以处理 3d 矩阵,但我不知道如何将其推广到 4d。

让我用一个例子更好地解释一下。

clear all

A(:,:,1,1)=[1 2 3; 4 5 6];
A(:,:,2,1)=[7 8 9; 10 11 12];
A(:,:,1,2)=[13 14 15; 16 17 18];
A(:,:,2,2)=[19 20 21; 22 23 24];

B=reshape(A,[2*3*2*2,1]);

假设changecoord(i,j,h,p) 是给出位置的函数 A(i,j,h,p) in B

那么,这个算法应该分裂

%changecoord(1,1,1,1)=1
%changecoord(2,1,1,1)=2
%changecoord(1,1,2,2)=19
%changecoord(1,2,2,2)=21 
%etc.

【问题讨论】:

标签: matlab


【解决方案1】:

在重塑时,数组的元素在内存中的顺序不会改变。因此,linear index 保持不变。您可以使用sub2ind(如rahnema1 suggested in a comment)从(i,j,h,p) 坐标中获取线性索引。

index = sub2ind(size(A),i,j,h,p);

现在,对于通过重塑A 获得的任何B

A(index) == B(index)

您可以使用ind2sub 检索B 中的坐标:

B = reshape(A,4,3,2);
[i,j,h] = ind2sub(size(B),index);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-25
    • 1970-01-01
    • 2017-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多