Performance – What is the fastest way to expand matrix in matlab?

How to turn the matrix:

[ 0.12 0.23 0.34 ;
0.45 0.56 0.67 ;
0.78 0.89 0.90 ]

Enter a’coordinates’ matrix with a bunch of rows?

[ 1 1 0.12 ;
1 2 0.23 ;
1 3 0.34 ;
2 1 0.45 ;
2 2 0.56 ;
2 3 0.67 ;
3 1 0.78 ;
3 2 0.89 ;
3 3 0.90 ]

(The arrangement of rows is irrelevant, only The data is only important in this structure)

Now I am using a for loop, but it takes a long time.

This is an option using ind2sub:

mat= [0.12 0.23 0.34 ;
0.45 0.56 0.67 ;< br /> 0.78 0.89 0.90] ;[I,J] = ind2sub(size(mat), 1:numel(mat));r=[I', J', mat(:)]r = 1.0000 1.0000 0.1200 2.0000 1.0000 0.4500 3.0000 1.0000 0.7800 1.0000 2.0000 0.2300 2.0000 2.0000 0.5600 3.0000 2.0000 0.8900 1.0000 3.0000 0.3400 2.0000 3.0000 0.6700 3.0000 3.0000 0.9000

Please note that the index is opposite compared to your example.

How to rotate the matrix:

[ 0.12 0.23 0.34 ;
0.45 0.56 0.67 ;
0.78 0.89 0.90 ]

Enter a’coordinates’ matrix with a bunch of rows?

[ 1 1 0.12 ;
1 2 0.23 ;
1 3 0.34 ;
2 1 0.45 ;
2 2 0.56 ;
2 3 0.67 ;
3 1 0.78 ;
3 2 0.89 ;
3 3 0.90 ]

(The arrangement of rows is irrelevant, only The data is important in this structure)

Now I am using a for loop, but it takes a long time.

This is a Options for using ind2sub:

mat= [0.12 0.23 0.34 ;
0.45 0.56 0.67 ;
0.78 0.89 0.90] ;[I,J ] = ind2sub(size(mat), 1:numel(mat));r=[I', J', mat(:)]r = 1.0000 1.0000 0.1200 2.0000 1.0000 0.4500 3.0000 1.0000 0.7800 1.0000 2.0000 0.2300 2.0000 2.0000 0.5600 3.0000 2.0000 0.8900 1.0000 3.0000 0.3400 2.0000 3.0000 0.6700 3.0000 3.0000 0.9000

Please note that the index is the opposite compared to your example.

Leave a Comment

Your email address will not be published.