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).

1
  • Using dynamic variable names is bad, read this answer of mine. Why don't you simply use a multidimensional array? Commented Sep 20, 2017 at 15:49

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.