Real and Imaginary element Separation from square matrix and stacking into a vector

Around 10000 matrices are stored in a folder in .mat format.
The matrices are square in nature with order NxN in my case N=15.
Important characteristics of the matrix
1) Diagonal elements D11,D22 etc are real in nature
2) The other elements in the matrices are complex in nature.
3) The elements in the lower triangle are equal to the complex conjugates of the upper triangle matrix, so the real part remains the same.
Requirement:
1) Separate all the diagonal elements and store in a column vector.
So Nx1 column vector-1
2) Separate all the Real parts of the upper triangle matrix and lower triangle matrix and store in another column vector.
Since for NxN matrix the number of elements the upper triangle is given by is N(N-1)/2 and similarly for lower triangle is given by N(N-1)/2
So in total 2 x (N(N-1))/2)= [N(N-1)] x1 column vector-2
3) At the end stacking a column vector-1 and column vector-2 to form a column vector-3 of N2x1 order.
Like-
Please help me in getting Column vector-3 which is very crucial. Manually could be done for few matrices but at present the task is tobe done for 10000 matrices, which may even increase. Thanks

 採用された回答

Jon
Jon 2020 年 11 月 10 日
編集済み: Jon 2020 年 11 月 10 日
D = diag(A)
L = triu(A,1)
U = tril(A,-1)
[D; real(U(:)); real(L(:))]
is close but you still have zero elements in L and U to get rid off
If you know that none of the original elements are zero you could use
D = diag(A)
L = triu(A,1)
L = L(L~=0)
U = tril(A,-1)
U = U(U~=0)
[D; real(U); real(L)]

13 件のコメント

Jon
Jon 2020 年 11 月 10 日
Sorry had a typo in original answer, I just edited it
Jon
Jon 2020 年 11 月 10 日
Here's a better way to do it given a N by N matrix A
i = 1:N
j = 1:N
u = A(i > j')
l = A(i < j')
d = A(i==j')
V = [d; real(u); real(l)]
Karthik Nagaraj
Karthik Nagaraj 2020 年 11 月 11 日
Thank you Jon!
The last solution works fine!
Jon
Jon 2020 年 11 月 11 日
Excellent, glad ths was what you wanted. It took me a little while to realize that you could make a square logical matrix by comparing terms in a row vector with a column vector in this case i is the row and j' is the column
Karthik Nagaraj
Karthik Nagaraj 2020 年 11 月 11 日
Also, I used this to code to fetch mat files in the folder.
files = dir('*.mat');
for i = 1:numel(files)
filename = files(i).name;
data = load(filename);
As I have 10000 matrices stored in a folder in .mat format. Could you help me how to perform the same 3 tasks on those 10000 matrices, which is shown by you for a single A matrix and save those vectors in another folder
Jon
Jon 2020 年 11 月 11 日
Hi you could store the results in a single matrix with each column coming from one of your data files, so something like this
N = % put in the dimension of your individual matrix here
files = dir('*.mat');
% determine how many files there are
numFiles = numel(files)
% preallocate an array to hold the results
results = zeros(N^2,numFiles); % each column will have N^2 entries
for i = 1:numel(files)
% load the data from the current file
filename = files(i).name;
data = load(filename);
% just get the A matrix (or whatever you call it in the .mat file)
A = data.A;
% create result vector
D = diag(A);
L = triu(A,1);
L = L(L~=0);
U = tril(A,-1);
U = U(U~=0);
A(:,i) = [D; real(U); real(L)]; % store the result
end
Karthik Nagaraj
Karthik Nagaraj 2020 年 11 月 18 日
Thanks a lot Jon for your one more answer.
% just get the A matrix (or whatever you call it in the .mat file)
A = data.A;
This above line is displaying error while executing
Dot indexing is not supported for variables of this type.
Should it be changed to struct format?
Jon
Jon 2020 年 11 月 18 日
I'm not quite sure what problem you are having, but I think it has to do with how you load the data. Note that if you use
data = load(filename);
that is assign the results of loading to a variable, then that variable will be a structure whose fields are the various variables stored in the .mat file.
If you just use
load(filename);
that is no assigment to a left hand side variable then the variables in the .mat file will be loaded directly into the workspace. So then data.A for example would have no meaning as there would not be any structure
Karthik Nagaraj
Karthik Nagaraj 2020 年 11 月 18 日
編集済み: Karthik Nagaraj 2020 年 11 月 18 日
Thank you Jon for your swift reply. I could solve the error based on your answer
My typical code looks like this. I have generated and stored the complex matrices in a mat file named A.
clear all;
close all;
clc;
N=15;
C1= rand(N,N) + 1i*rand(N,N); % Complex Matrix
C2= rand(N,N) + 1i*rand(N,N); % Complex Matrix
C3= rand(N,N) + 1i*rand(N,N); % Complex Matrix
C4= rand(N,N) + 1i*rand(N,N); % Complex Matrix
save('A.mat','C1','C2','C3','C4')
files = dir('*.mat');
% determine how many files there are
numFiles = numel(files)
% preallocate an array to hold the results
results = zeros(N^2,numFiles); % each column will have N^2 entries
for i = 1:numel(files)
% load the data from the current file
filename = files(i).name;
data = load(filename);
% just get the A matrix (or whatever you call it in the .mat file)
A = data.A;
% create result vector
D = diag(A);
L = triu(A,1);
L = L(L~=0);
U = tril(A,-1);
U = U(U~=0);
A(:,i) = [D; real(U); real(L)]; % store the result
end
I have encountered a new error now :
Reference to non-existent field 'A'.
A = data.A;
eventhough A has been defined. Could you help me understand this
Jon
Jon 2020 年 11 月 18 日
It looks to me like you only have one data file. It seems to be called A.mat. Iniside of this file there seem to be 4 variables stored C1,C2,C3,C4 when you load it using A = load(filename) I think you are getting back a structure with A.C1, A.C2, A.C3, A.C4
So maybe you have to look more into the documentation for save and load and figure out exactly what you are trying to do here.
Karthik Nagaraj
Karthik Nagaraj 2020 年 11 月 19 日
Hi Jon, I tried another apporach using for loops and saved the data in a cell.
clear all;
close all;
clc;
N=15;
k=6;
C=cell(k,1)
for j = 1:k
C{j}= rand(N,N) + 1i*rand(N,N); % Complex Matrix
end
Now the cell C contains 6 x1 data with each each cell has 15x15 data in 6 cell.
Is it better to split the cell and perform the operations
D = diag(C)
L = triu(C,1)
U = tril(C,-1)
[D; real(U(:)); real(L(:))]
Jon
Jon 2020 年 11 月 19 日
Yes, better to just keep the data within the local workspace and utilize existing programming elements such as cell arrays. I wasn't sure why you were saving the data in files. I thought that was just to simulate the real application where you had to use some experimental data that had been stored in files.So are you all set now?
Karthik Nagaraj
Karthik Nagaraj 2020 年 11 月 19 日
What you thought in the last two sentences are perfectly accurate. Thanks a lot for your patience. I hope I am all set now. If something is there, I will come back. Thanks

サインインしてコメントする。

その他の回答 (1 件)

Setsuna Yuuki.
Setsuna Yuuki. 2020 年 11 月 10 日
with a matrix of example "a":
a = [1 1+3*i 2+3*i; 3+4*i 2 4+4*i; 5+4*i 6+4*i 3];
realMatrix = zeros(1,length(a));
realMatrixx = zeros(1,(length(a)*(length(a)-1))/2);
k=1; l = 1;
for i=1:length(a)
for j=1:length(a)
if(a(i,j) == real(a(i,j))) %Only if real (a+0*j)
realMatrix(k) = a(i,j);
k=k+1;
elseif(a(i,j) == complex(a(i,j))) %only is complex (a+b*j)
realMatrixx(l) = real(a(i,j));
l=l+1;
end
end
end
realMatrix=realMatrix'; %V1
realMatrixx=realMatrixx'; %V2
final = [realMatrix;realMatrixx]; %V3

2 件のコメント

Karthik Nagaraj
Karthik Nagaraj 2020 年 11 月 11 日
Thank you! solution works fine!
Karthik Nagaraj
Karthik Nagaraj 2020 年 11 月 11 日
Also, I used this to code to fetch mat files in the folder.
files = dir('*.mat');
for i = 1:numel(files)
filename = files(i).name;
data = load(filename);
As I have 10000 matrices stored in a folder in .mat format. Could you help me how to perform the same 3 tasks on those 10000 matrices, which is shown by you for a single A matrix and save those vectors in another folder

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

質問済み:

2020 年 11 月 10 日

コメント済み:

2020 年 11 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by