简体   繁体   English

可以在GNU Octave中将向量用作3D数组索引吗?

[英]Is it possible to use a vector as a 3D array index in GNU Octave?

Would you please tell me, whether it is possible to use a vector as a 3D array index in GNU Octave? 您能否告诉我,在GNU Octave中是否可以将向量用作3D数组索引? Ideally I would like to use vector v as an index of 3D arrays d and g , ie refer to a specific array element as d(v) and g(v) , instead of using a cumbersome expression d(v(1),v(2),v(3)) . 理想情况下,我想将向量v用作3D数组dg的索引,即将特定的数组元素称为d(v)g(v) ,而不要使用繁琐的表达式d(v(1),v(2),v(3)) There must be a more elegant way. 必须有更优雅的方式。 Please see an example below: 请参见下面的示例:

% specify coordinate shift to ensure positive indices
cs = [ni nj nk]

% loop over source cell coordinates
for i1 = 1:ni
  for j1 = 1:nj
    for k1 = 1:nk

    % specify source cell coordinates
    r1 = [i1 j1 k1];

    % loop over test cell coordinates
    for i2 = 1:ni
      for j2 = 1:nj
        for k2 = 1:nk

          % specify test cell coordinates
          r2 = [i2 j2 k2];

          % calculate index vector
          v = r1-r2+cs;

          % calculate Euclidean distance between source and test cell centres
          d(v(1), v(2), v(3)) = norm(r1-r2);
          g(v(1), v(2), v(3)) = exp(-img*k*d(v(1), v(2), v(3))) / 4.0 / pi / d(v(1), v(2), v(3));

        endfor
      endfor
    endfor

    endfor
  endfor
endfor

I don't think it is possible. 我认为不可能。 You could write a intermediary function to make sub2ind achieve it though. 您可以编写一个中介函数来使sub2ind实现它。

function ind = vec2ind(sz, vec)
    ind = sub2ind(sz, vec(1), vec(2), vec(3)); %//although even better to go vec(:,1) etc...
end

and now you can replace d(v(1), v(2), v(3)) with d(vec2ind(v)) but is that really any better? 现在您可以将d(v(1), v(2), v(3))替换为d(vec2ind(v))但这真的更好吗? Probably best to just accept Octave's syntax on this one. 最好只接受此语言的Octave语法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM