How to extract value from a matrix with given indices
    25 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    Shin
 el 17 de En. de 2023
  
    
    
    
    
    Comentada: Shin
 el 17 de En. de 2023
            Hi there,I have a an array of index and a matrix such as
index = [2 4 3 1 5];
matrix = [5 2 5 3 4;
          1 2 4 2 6;
          7 5 0 9 3;
          6 6 3 1 2;
          3 6 8 2 7];
How can I extract the value from each column of the matrix with the given index to obtain a new array such as
new = [1 6 0 3 7];
Thanks.
-Shin-
0 comentarios
Respuesta aceptada
  Dyuman Joshi
      
      
 el 17 de En. de 2023
        index = [2 4 3 1 5];
matrix = [5 2 5 3 4;
          1 2 4 2 6;
          7 5 0 9 3;
          6 6 3 1 2;
          3 6 8 2 7];
s=size(matrix);
new=matrix(sub2ind(s,index,1:s(2)))
Más respuestas (2)
  prasanth s
      
 el 17 de En. de 2023
        code example is
ind = sub2ind(size(matrix),index,1:5);
new=matrix(ind)
Ver también
Categorías
				Más información sobre Matrix Indexing en Help Center y File Exchange.
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




