1

I want to receive the output of [I_1,I_2,...,I_n] = ind2sub(siz,IND) for an n-dimensional array with size of dimensions defined in siz. The linear index is a single value in IND. I know the number of elements in each dimension of the array. But the number of dimensions is variable (it is known but variable). I want to know how to receive the output of ind2sub(siz,IND) in variable number of arrays as it needs n number of arrays to receive the output.

In fact, the number of dimensions is number of attributes of data points in a data set. If data set is called x the number of dimensions is size(x,2).

gnovice
  • 125,304
  • 15
  • 256
  • 359
Masoud
  • 305
  • 1
  • 12
  • Using dynamic variable names is bad, [read this answer of mine](https://stackoverflow.com/a/32467170/5211833). Why don't you simply use a multidimensional array? – Adriaan Sep 20 '17 at 15:49

1 Answers1

1

To collect an arbitrary number of subscripted indices from ind2sub, you'll want to use a cell array instead of individual variables like I_1, I_2, etc., to store the output. You can capture the comma-separated list output as follows, assuming your N-dimensional data is in a variable x:

[indices{1:ndims(x)}] = ind2sub(size(x), IND);

And indices will now be a 1-by-ndims(x) cell array containing the subscripts for each dimension that correspond to the linear indices in IND.

gnovice
  • 125,304
  • 15
  • 256
  • 359