ind2sub with arbitrary martix

10 views (last 30 days)
Thomas
Thomas on 16 Feb 2023
Commented: Lucas Graham on 7 Jun 2023
Hi
I intend to use ind2sub to get the subscripts indexing from linear indexing and I will use if to read data in matrix of arbitrary size.
The problem with this is that you have to know the dimension of the matrix, so that you can call ind2sub with the corresponding number of outputs. What I would like is to get a output vector that has the corresponding number of elements. Is this possible to achieve?
Best regards
Thomas

Accepted Answer

Stephen23
Stephen23 on 16 Feb 2023
Edited: Stephen23 on 16 Feb 2023
"In the example below: how to set the three output arguments [I1 I2 I3] "automatically" ?"
You use a comma-separated list:
A = rand(3,6,34);
X = 24;
C = cell(1,ndims(A));
[C{:}] = ind2sub(size(A),X);
Checking:
C{:}
ans = 3
ans = 2
ans = 2
  4 Comments
Stephen23
Stephen23 on 16 Feb 2023
Edited: Stephen23 on 16 Feb 2023
"Strange that the output argument from int2sub is not automatically of size 1 x ndims(A) "
X can be any size, the outputs have exactly the same size as X. The proposed output size would conflict with that.
If you restrict X to scalar only, then the function is very easy to copy and modify.
Lucas Graham
Lucas Graham on 7 Jun 2023
I was about to try to gimmick some sort of iteratively generated string and then use eval(), this is so much better. Thanks.

Sign in to comment.

More Answers (3)

Torsten
Torsten on 16 Feb 2023
You mean
A = rand(5,6);
s = size(A)
s = 1×2
5 6
?
  2 Comments
Thomas
Thomas on 16 Feb 2023
[i1,i2,i3]=ind2sub([3,3,3],8)
i1 = 2
i2 = 3
i3 = 1
[i1,i2,i3,i4]=ind2sub([3,3,3,4],8)
i1 = 2
i2 = 3
i3 = 1
i4 = 1
But I would like to have a function my_ind2sub so that
I=my_ind2sub([3,3,3],8)
->I=[2,3,1]
and
I=my_ind2sub([3,3,3,3],8)
->I=[2,3,1,1]
Torsten
Torsten on 16 Feb 2023
I don't know how you automativcally get I according to the matrix size. I asked this question below. I think @Steven Lord will be able to answer this.

Sign in to comment.


dpb
dpb on 16 Feb 2023
Moved: dpb on 16 Feb 2023
Use size(M) as the size argument to ind2sub where M is the matrix...the size argument doesn't have to be a constant expression.

Steven Lord
Steven Lord on 16 Feb 2023
You have to specify the size of the array you want to use the subscripts to index into because the same linear index could generate different subscripts based on the size. Consider two examples where I want the subscripts for element 4.
A1 = reshape(1:8, 2, 4)
A1 = 2×4
1 3 5 7 2 4 6 8
A2 = reshape(1:8, 4, 2)
A2 = 4×2
1 5 2 6 3 7 4 8
A1 and A2 have the same number of elements but different sizes. This means that element 4 of A1 is element (2, 2) and element 4 of A2 is element (4, 1).
[r1, c1] = ind2sub(size(A1), 4)
r1 = 2
c1 = 2
[r2, c2] = ind2sub(size(A2), 4)
r2 = 4
c2 = 1
If I just called ind2sub with the input 4, should the outputs be 2 and 2, 4 and 1, or something else?
But let's take a step back. When you say this:
What I would like is to get a output vector that has the corresponding number of elements.
do you just want to use linear indexing? How are you hoping or planning to use these indices with a "matrix of arbitrary size"?
B1 = A1.^2 % just so the elements aren't 1:8
B1 = 2×4
1 9 25 49 4 16 36 64
B2 = A2.^2
B2 = 4×2
1 25 4 36 9 49 16 64
[B1(4) B2(4)] % Both are 16
ans = 1×2
16 16
  1 Comment
Torsten
Torsten on 16 Feb 2023
Say I have a matrix A I don't know the size of in advance.
How can I prescribe that the array of outputs I1,I2,...In from ind2sub as
matches the number of dimensions of A ?
In the example below: how to set the three output arguments [I1 I2 I3] "automatically" ?
A = rand(3,6,34);
s = size(A);
s = 1×3
3 6 34
[I1 I2 I3] = ind2sub(s,24)
I1 = 3
I2 = 2
I3 = 2

Sign in to comment.

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!