【问题标题】:MATLAB: How do you call a "cross-section" of an m-dimensional array?MATLAB:如何称呼 m 维数组的“横截面”?
【发布时间】:2013-05-30 07:40:50
【问题描述】:

在 MATLAB 中,我有一个多维浮点数组 A,维度为 m;也就是说,它的条目可以用A(n_1, n_2, ..., n_m)引用。

我不知道有没有什么好的方法可以用技术术语来描述,所以我举个例子:如果A是一个4维数组,那么

A(:,:,1,:)A 在第三rd 坐标中的第一st 横截面。同样,

A(:,2,1,:) 将是A 在第 2nd 和 3rd 坐标中的 (2,1)th 横截面.

所以我的一般问题是,给定一个 A,其尺寸仅在运行时确定,我如何在 (c_1,...,c_j) 坐标中引用A(k_1,...,k_j)th 横截面,其中kc 也是变量?

【问题讨论】:

  • 我问了一个类似的问题here,但现在看到它并没有立即解决您的问题。忽略我对复制/关闭的投票

标签: arrays matlab multidimensional-array


【解决方案1】:

您需要使用元胞数组索引A

% Create array
A = rand(4,4,4,4);

% example k & c
k = [3 4 4];
c = [1 3 4];   


% Things that can go wrong
szA = size(A);
if numel(k) ~= numel(c) || any(c > ndims(A)) || any(k > szA(c)) 
    error('Invalid input.'); end


% Create the cell { ':' ':' ':' ... } with 
% the correct amount of repetitions
R = repmat({':'}, 1,ndims(A));

% Change it to { [3] ':' [4] [4] } 
% (depending on k and c of course)
R(c) = num2cell(k);

% use it to reference A
A(R{:})

【讨论】:

    【解决方案2】:

    我没有 matlab,所以我无法确认,但我怀疑答案是:

    function [val] = get_cross_value(A, cross_section, coord) {
         cross_A =  squeeze(A(cross_section{:}));
         val = squeeze(cross_A(coord));
    } 
    

    我的信息来自http://www.mathworks.com/matlabcentral/newsreader/view_thread/166539

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-15
      • 2018-08-19
      • 1970-01-01
      • 1970-01-01
      • 2016-07-02
      • 1970-01-01
      相关资源
      最近更新 更多