How to extract diagonal elements of multiple nxn square matrix and place them in single mat file or matrix?
Ältere Kommentare anzeigen
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 Kommentare
Matt J
am 27 Sep. 2018
Why not just use a loop?
Guillaume
am 27 Sep. 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
am 27 Sep. 2018
Stephen23
am 28 Sep. 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
am 28 Sep. 2018
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Operating on Diagonal Matrices finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!