Real and Imaginary element Separation from square matrix and stacking into a vector
2 次查看(过去 30 天)
显示 更早的评论
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/409745/image.png)
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-
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/409750/image.png)
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
0 个评论
采纳的回答
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
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?
更多回答(1 个)
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!