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 Comments

Hello Matt,
I am able to extract the diagonal elements and transpose the extracted elements so that the dimensions are 1x10 and store this in diagonal_mat variable. But my question is how to add the next matrix diagonal elements in a next row of the diagonal_mat variable?
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.
Thanks, It worked
clc;
mat_output=[];
for k = 1:7783
if exist(['subjecttID_', num2str(k),'_Eigen','.mat'],'file')
dat = load(['SubjecttID_', num2str(k),'_Eigen','.mat']);
% ------------ save data naming -------------- %
Dia = diag(dat.D);
Dia = transpose(Dia);
mat_output = [mat_output;Dia];
end
end
save('test','mat_output')
@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.
Thanks @Stephen. Yes, I used ytzhak answer for this issue and accepted his answer

Sign in to comment.

 Accepted Answer

Hope this helps
step1:creat a nXnXm with random elements for the example:
%set dimensions of matrix
size_of_matrix=10;
number_of_matricides=500;
mat_input=randi(30,size_of_matrix,size_of_matrix,number_of_matricides);
step2: initialize an output matrix with nXm dimensions
mat_output=zeros(number_of_matricides,size_of_matrix);
step3: extract a diagonal from each matrix and place them in the output matrix
for i=1:number_of_matricides
mat_output(i,:)=diag(mat_input(:,:,i));
end
Alternatively, though not recommended, you can simply concatenate:
%initialize an empty matrix
mat_output=[];
for i=1:number_of_matricides
mat_output=[mat_output;diag(mat_input(:,:,i))'];
end

5 Comments

Note that step 2 and step 3 can be altogether replaced by:
mat_output = reshape(mat_input(repmat(logical(eye(size(mat_input, 1))), 1, 1, size(mat_input, 3))), [], size(mat_input, 3));
I agree, but i don't think that it would have been clear
By far (and I really do mean far, we're talking sun size vs atom size), your biggest problem is the repeated rereading of each file. I don't know how many of the 389,150 files you're trying to read do exist, but if we call that number of files that exist N, you're reading each file N^2+N times when you should be reading each only once (so N read in total). If all 389,150 files do exist, you're doing 151,437,722,500 unnecessary file read (more than 151 billions!). Does your program ever finish?
This is easily fixed with some major rework of your code. However, this warrants its own question. So please start a new question with the above code (and put a link to it as a comment below).
Note that if these 389,150 files that you're reading are the diagonals of the original mat files, then as I wrote as a comment to your question don't do that. Separating your data into many files makes everything more complicated and slower.
@yzthak,
Just reread your answer in more details, and I noticed something that made me chuckle:
You may want to read the definition of matricide. I believe you meant matrices
Thanks for the comment, I figured out that I need to create a struct with all the variables and access it. I already created a question and the link is New_Question

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2018a

Asked:

on 27 Sep 2018

Edited:

on 21 Oct 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!