How to extract diagonal elements of multiple nxn square matrix and place them in single mat file or matrix?
Mostrar comentarios más antiguos
Hello,
I have 500 mat files with 10x10 double dimension. I want to extract the 10 diagonal elements from each matrix and place them in a single matrix which will be 500x10 (500 rows and 10 columns) each row consists of diagonal elements of one matrix. How can I make a single variable with all the diagonal elements?
Thanks
6 comentarios
Matt J
el 27 de Sept. de 2018
Why not just use a loop?
Guillaume
el 27 de Sept. de 2018
how to add the next matrix diagonal elements
The proper term for what you want to do is concatenate not add. Concatenation is done with horzcat or simply with [] (Addition is +).
Concatenation is trivial to do. What's not clear and may greatly complicate what you're trying to do is how the matrices are stored. Are each stored in individual mat files (.mat files) or have you got all of them in your workspace (hopefully not as individual variables).
Certainly if they're stored in separate mat files, the thing not to do is store their diagonal in even more separate mat files (a complete waste of time). The simplest thing to do would be to concatenate the matrices from the mat files into a 10x10x500 matrix from which extracting the diagonals all at once would be trivial.
Sunny
el 27 de Sept. de 2018
Stephen23
el 28 de Sept. de 2018
@Varun Mandalapu: note that concatenating onto the output array is not very efficient. For a more efficient solution you should preallocate the output array and use indexing, as ytzhak goussha's answer shows.
Sunny
el 28 de Sept. de 2018
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Operating on Diagonal Matrices en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!