【问题标题】:Extract column of matrices from a 3D indexes从 3D 索引中提取矩阵列
【发布时间】:2017-11-29 07:15:22
【问题描述】:

我尝试从一组给定的索引(3D)中提取矩阵列(2D),而不将 2D 矩阵转换为 3D 以便有效地使用内存(我正在处理非常庞大的数据),可以我实现了这个?我将为 3D 矩阵的每个切片重用 2D 矩阵,但我只是不知道该怎么做。示例代码:

A=rand(9,100); %The matrix that will be reused
B=randi([1 100],[1 100 30]); %the indices
Extracted=A(:,B); %this part I can't seem to solve it yet

Extracted 的预期输出将是 9x100x30。有什么想法吗?提前致谢!

【问题讨论】:

    标签: matlab matrix 3d


    【解决方案1】:

    一个解决方案可以使用内联函数和arrayfun:

    A=rand(9,100); %The matrix that will be reused
    B=randi([1 100],[1 100 30]); %the indices
    func = @(x) A(x);
    Extracted = arrayfun(func, B);
    

    另一种解决方案是将 3D 矩阵 B 转换为向量。 然后用你的方法检索Extracted

    A=rand(9,100); %The matrix that will be reused
    B=randi([1 100],[1 100 30]); %the indices
    idx = reshape(B,1,numel(B));
    Extracted = A(idx);
    

    【讨论】:

    • 使用第一种方法,Extracted 的输出将是1x100x30,但它错过了其他 8 行。
    • @GregorIsack 如果B的元素是线性索引,所以不会发生。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-23
    • 2018-09-17
    • 2018-05-12
    • 1970-01-01
    • 1970-01-01
    • 2015-02-20
    • 1970-01-01
    相关资源
    最近更新 更多