Ran across this problem when trying to simulate IPF–iterative proportional fitting for my STATS HW.

I was trying to construct a 7-dimensional matrix that represents clique potentials for 7 nodes. I found little resources on the web talking about dealing with high dimensional matrices in MATLAB (e.g. index specification, dimension expansion/reduction). So I just want to share some of the tricks/approaches we can use when having high dimensional (N>3) data in MATLAB.

I’m sure that there should be better solutions to address what I’m posting here, and any sharing and comment would be greatly appreciated.

Imagine we have a 2 by 2 by 2 by 2 by 2, a 5-dimensional matrix A, in MATLAB,

size(A)

would return

2 2 2 2 2

It’s usually simple to think of or visualize 2 or 3 dimensional data; but it’d be hard to create a high dimensional data image in mind.

- How to automate the process of extracting only the n
^{th} dimension from A?

If we have a 2D matrix:

B = [1,2;3,4]

B(:,1)

would give us all the elements in the first dimension of B, in this case it’s

[1 2]

It’s not hard to think that for higher dimension that still works.

A(:,1,1,1,1)

would give us all the elements in the first dimension of A. Then the problem comes when we want to automate this extraction process. How can we make this into a for loop? There is a rather complicated way to do this. It works, but I’m sure that there could be better solutions. Hope someone can point it out.

We first create a cell with length same as the matrix dimension. This cell would store the index string we are creating.

ind = cell{1,5}

Use a for loop to set all values to

for i = 1:5
ind{i} = {[1]};
end

The brace and brackets are important here. Each element within the cell array is a cell. Then we set the dimension we want to address to have the character:

':'
ind{dimension_addressed} = {':'};

The last step is to use this cell array to address the specific dimension we are referring to:

A(ind{:})

would be the correct way to automatically address the desired dimension in a high dimensional matrix in MATLAB.

### Like this:

Like Loading...

*Related*