MATLAB: What do you call the "cross section" of an m-dimensional array?

In MATLAB, I have a multidimensional array of floats, A , with sizes m ; that is, his entries may refer to A(n_1, n_2, ..., n_m) .

I don't know if there is a good way to describe this with technical terms, so let me just use an example: if A is a 4-dimensional array, then

A(:,:,1,:) is the section 1 st A at the coordinate 3 rd . Similarly

A(:,2,1,:) would be the section (2,1) th A at the coordinates 2 nd and 3 rd .

So, my general question is: given A , whose dimensions are determined only at runtime, how can I refer to the section (k_1,...,k_j) th A in the coordinates (c_1,...,c_j) , where k and c are also variables?

+4
source share
2 answers

You need to index A using an array of cells:

 % 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{:}) 
+1
source

I don't have MATLAB, so I cannot confirm this, but I suspect the answer is:

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

My information is obtained from http://www.mathworks.com/matlabcentral/newsreader/view_thread/166539

0
source

Source: https://habr.com/ru/post/1484323/


All Articles