How to extract diagonal elements of multiple nxn square matrix and place them in single mat file or matrix?
古いコメントを表示
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 件のコメント
Matt J
2018 年 9 月 27 日
Why not just use a loop?
Guillaume
2018 年 9 月 27 日
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
2018 年 9 月 27 日
Stephen23
2018 年 9 月 28 日
@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
2018 年 9 月 28 日
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Operating on Diagonal Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!