-1

I'm trying to realise the following Octave command in MATLAB:

M = eye(x)(y,:);

x is just a number (in my example 10) and y is a vector (here 8x1):

y = [1 3 4 5 7 10 9 10];

The Octave command would generate:

M =

   1   0   0   0   0   0   0   0   0   0    
   0   0   1   0   0   0   0   0   0   0    
   0   0   0   1   0   0   0   0   0   0    
   0   0   0   0   1   0   0   0   0   0    
   0   0   0   0   0   0   1   0   0   0    
   0   0   0   0   0   0   0   0   0   1    
   0   0   0   0   0   0   0   0   1   0    
   0   0   0   0   0   0   0   0   0   1

The ones are kept very near to the diagonal.

The nearest I came with MATLAB is with the following commands:

n = size(y,1);
Y = eye(n, x);

but it would generate something still different. If the difference between rows and columns gets bigger, it would be very different.

M =

   1   0   0   0   0   0   0   0   0   0
   0   1   0   0   0   0   0   0   0   0
   0   0   1   0   0   0   0   0   0   0
   0   0   0   1   0   0   0   0   0   0
   0   0   0   0   1   0   0   0   0   0
   0   0   0   0   0   1   0   0   0   0
   0   0   0   0   0   0   1   0   0   0
   0   0   0   0   0   0   0   1   0   0

How could I get the first matrix with MATLAB?

m7913d
  • 10,244
  • 7
  • 28
  • 56

1 Answers1

0

First you should find what this expression eye(x)(y,:) means. First create an identity matrix with the size of x by x, and then select rows with index in y. Therefore, the equivalent syntax would be:

 E = eye(x);
 M = E(y,:);
OmG
  • 18,337
  • 10
  • 57
  • 90