从Matrix 3D获得矩阵2D,并给定的第三维的给定选择与第一个维度相对应

发布于 2025-01-23 19:27:24 字数 476 浏览 1 评论 0原文

我有:

  • a矩阵3D:a =(m,n,k)。

  • 与第一个维度的每个索引相对应的第三维的选择数组。 idn =(m,1)(其中任何IDN的值是[1,k]中的随机整数。

我需要捕获2D矩阵B(m,n),其中所述的第三维是从相应的例如,

idn(1) = 1;
idn(2) = k;
idn(j) = k-1;

由于

B(1,:) = A(1,:,idn(1)) = A(1,:,1);
B(2,:) = A(2,:,idn(2)) = A(2,:,k);
B(j,:) = A(j,:,idn(j)) = A(j,:,k-1);

不是常数,因此

我也尝试了以下代码。

B = A(:,:,idn(:));

IDN 给我解决方案。

I have:

  • A matrix 3D: A = (m, n, k).

  • An array of choices for the third dimension corresponding to each index of the first dimension. idn = (m, 1) (wherein the value of any idn is a random integer in [1,k].

I need to capture the 2D matrix B (m,n) wherein the referred third dimension to A is taken from the corresponding choice. For example:

idn(1) = 1;
idn(2) = k;
idn(j) = k-1;

Then:

B(1,:) = A(1,:,idn(1)) = A(1,:,1);
B(2,:) = A(2,:,idn(2)) = A(2,:,k);
B(j,:) = A(j,:,idn(j)) = A(j,:,k-1);

Since idn is not constant, a simple squeeze could not help.

I have also tried the below code, but it does not work either.

B = A(:,:,idn(:));

It is very much appreciated if anyone could give me a solution.

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

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

发布评论

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

评论(1

七色彩虹 2025-01-30 19:27:24

这可以用 sub2ind premute ,但是我是最简单的方法我可以想到的是使用 linearear indexing 手动手动:

A = rand(3, 4, 5); % example data
idn = [5; 1; 2];   % example data
ind = (1:size(A,1)).' + size(A,1)*size(A,2)*(idn(:)-1); % 1st and 3rd dimensions
ind = ind + size(A,1)*(0:size(A,2)-1); % include 2nd dimension using implicit expansion
B = A(ind); % index into A to get result

This could be done with sub2ind and permute, but the simplest way I can think of is using linear indexing manually:

A = rand(3, 4, 5); % example data
idn = [5; 1; 2];   % example data
ind = (1:size(A,1)).' + size(A,1)*size(A,2)*(idn(:)-1); % 1st and 3rd dimensions
ind = ind + size(A,1)*(0:size(A,2)-1); % include 2nd dimension using implicit expansion
B = A(ind); % index into A to get result
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文