What is the fastest way to deploy a matrix in MATLAB?

How to transform a matrix:

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

into a "coordinate" matrix with a group 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 ]

(row permutation does not matter, it is only important that the data is in this structure)

I am currently using a for loop, but it takes a lot of time.

+3
source share
4 answers

Here is an option 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

Note that indexes are reversed compared to your example.

+9
source
A = [ .12 .23 .34 ;
      .45 .56 .67 ;
      .78 .89 .90 ];

[ii jj] = meshgrid(1:size(A,1),1:size(A,2));
B = A.';
R = [ii(:) jj(:) B(:)];

If you don't mind a different order (according to your editing), you can do this more easily:

[ii jj] = ndgrid(1:size(A,1),1:size(A,2));
R = [ii(:) jj(:) A(:)];
+6
source

/ meshgrid find :

[II,JJ,AA]= find(A.'); %' note the transpose since you want to read across
M = [JJ II AA]
M =
            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.9

, . , ( user664303):

B = A.'; v = B == 0; %' transpose to read across, otherwise work directly with A
[II, JJ, AA] = find(B + v);
M = [JJ II AA-v(:)];

, .:) , ndgrid , col inds.

+5

ndgrid , kron repmat:

M = [kron(1:size(A,2),ones(1,size(A,1))).' ...  %' row indexes
     repmat((1:size(A,1))',size(A,2),1) ...     %' col indexes
     reshape(A.',[],1)]                         %' matrix values, read across

, MATLAB:

M = [repmat((1:size(A,1))',size(A,2),1) ...     %' row indexes (still)
     kron(1:size(A,2),ones(1,size(A,1))).' ...  %' column indexes
     A(:)]                                      %  matrix values, read down

(, .)


I also find it a krongood tool to replicate each element at a time, and not the entire array at a time, like repmat. For instance:

>> 1:size(A,2)
ans =
     1     2     3
>> kron(1:size(A,2),ones(1,size(A,1)))
ans =
     1     1     1     2     2     2     3     3     3

Taking this a little further, we can generate a new function called repelfor replicating array elements, unlike the whole array:

>> repel = @(x,m,n) kron(x,ones(m,n));
>> repel(1:4,1,2)
ans =
     1     1     2     2     3     3     4     4
>> repel(1:3,2,2)
ans =
     1     1     2     2     3     3
     1     1     2     2     3     3
+1
source

All Articles