在 MATLAB 中使用两个具有分类值的映射向量将 2D 矩阵转换为 3D

发布于 2024-11-15 13:46:42 字数 796 浏览 3 评论 0原文

类似的问题可能已经被问过,但我找不到它。今天我遇到了一些问题,我找不到一个好的解决方案来解决一个常见的问题。

我有 M x N 双精度矩阵和两个带有分类值的 N x 1 向量(字符串元胞数组)。第一个向量包含 K 个唯一值和第二个 - L 个唯一值,例如 K * L >= N。

我想将原始矩阵转换为 3D 矩阵 M x K x L。因此,为了保留第一维,第二维将对应于第一向量中的唯一值,第三维将对应于第二向量中的唯一值。

让我们考虑向量没有排序。保证两个向量中对应元素不存在重复组合。缺失组合的矩阵值可以是 0。

我可以使用 grp2idx 将分类向量转换为数字,可以将其视为列号和页码。但如何将它们应用到原始矩阵上呢?

编辑:

这是一些随机数据:

A = reshape(1:24,4,6);
v1 = {'s1','s1','s2','s2','s3','s3'}';
v2 = {'t1','t2','t1','t2','t1','t2'}';
idx = randperm(6); %# just to randomize
v1 = v1(idx);
v2 = v2(idx);
[M, N] = size(z); %# M=4, N=6
K = numel(unique(v1)); %# K=3
L = numel(unique(v2)); %# L=2

我需要将矩阵 A 重塑为 4x3x2,使得第 1 列第 1 页对应于组合 s1_t1,第 2 列第 2 页对应于 s2_t2 等。在此示例中 K *L等于N,所以所有位置都会有数据,但这不是一般情况。

A similar question probably has been asked already, but I cannot locate it. Something wrong with me today, that I cannot find a good solution to a kind of frequent problem.

I have M x N matrix of doubles, and two N x 1 vectors (cell arrays of strings) with categorical values. The 1st vector contains K unique values and the 2nd - L unique values, such as K * L >= N.

I would like to convert the original matrix to 3D matrix M x K x L. So, to keep the 1st dimension, the 2nd dimension will correspond to unique values in the 1st vector, and 3rd dimension - to unique values in 2nd vector.

Let's consider that vectors are not sorted. It is guaranteed that there are no duplicate combinations of corresponding elements in two vectors. The matrix values for missing combinations can be 0s.

I can convert categorical vectors to numbers with grp2idx, which can be considered as column and page numbers. But how to apply them to the original matrix?

EDIT:

Here is some random data:

A = reshape(1:24,4,6);
v1 = {'s1','s1','s2','s2','s3','s3'}';
v2 = {'t1','t2','t1','t2','t1','t2'}';
idx = randperm(6); %# just to randomize
v1 = v1(idx);
v2 = v2(idx);
[M, N] = size(z); %# M=4, N=6
K = numel(unique(v1)); %# K=3
L = numel(unique(v2)); %# L=2

I need to reshape matrix A to 4x3x2 in such manner that column 1 page 1 will correspond to combination s1_t1, column 2 page 2 to s2_t2, etc. In this example K*L equals N, so all the positions will have the data, but this is not a general case.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

筑梦 2024-11-22 13:46:42

您可以使用 Accumarray 来实现此目的。请注意,grp2idx 会产生一些意想不到的结果,因为它从找到的第一个唯一元素开始编号,而不是根据 v1v2< 的排序值进行编号。 /代码>。如果您不关心顺序,可以使用例如idx2=grp2idx(v1)

idx1 = ndgrid(1:M,1:N); %# rows
[~,idx2]=ismember(v1,unique(v1)); 
idx2 = repmat(idx2',M,1); 
[~,idx3]=ismember(v2,unique(v2));
idx3 = repmat(idx3',M,1);

out = accumarray([idx1(:),idx2(:),idx3(:)],A(:),[M,K,L],@(x)x,0);

You can use accumarray for this. Note that grp2idx would give somewhat unexpected results, since it starts numbering at the first unique element it finds, instead of numbering according to sorted values of v1 or v2. If you don't care about order, you can use e.g. idx2=grp2idx(v1).

idx1 = ndgrid(1:M,1:N); %# rows
[~,idx2]=ismember(v1,unique(v1)); 
idx2 = repmat(idx2',M,1); 
[~,idx3]=ismember(v2,unique(v2));
idx3 = repmat(idx3',M,1);

out = accumarray([idx1(:),idx2(:),idx3(:)],A(:),[M,K,L],@(x)x,0);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文