Info

此问题已关闭。 请重新打开它进行编辑或回答。

How to store matrix from each iteration of a for loop in a larger matrix?

1 次查看(过去 30 天)
The question is pretty straight forward, I am getting a matrix as a result of each iteration of a for loop and I want to be able to store those results in a larger matrix. The way I had my code written, I was getting 49 3x3 matrices, but I wanted a 147x3 as my final result. I changed it so it would save each 3x3 as an element of a 49x1 matrix, but that gives me a result that has cells. I want to be able to separate the real and imaginary parts of my matrix at the end which I get an error for the way my code is now. If anyone has any thoughts, I would really appreciate it. I have copied my code below. Thanks! (Oh and the for loop in question is right at the end)
%Nyquist Curve to Determine System Stability Code
%Julia Hariharan 07/07/2020
clc;
clear all;
close all;
%Frequency from scan
Sweep;
%Line Impedance Calculation from Model
LineImpedanceCalculation
%Impedance from scan, converted to complex
[a,b]=pol2cart(Zp(:,3),Zp(:,2));
Zp=a+j*b
%Impedance Matrix Formulation
num6=0*F;
den6=1;
s6=num6./den6
sys6=s6.'
num7=0*F;
den7=1;
s7=num7./den7
sys7=s7.'
%Node 1
Z11=Zp+sys1+sys3
Z12=sys1+sys6+sys7
Z13=sys3+sys6+sys7
%Node 2
Z21=sys1+sys6+sys7
Z22=Zp+sys2+sys1
Z23=sys2+sys6+sys7
%Node 3
Z31=sys3+sys6+sys7
Z32=sys2+sys6+sys7
Z33=Zp+sys2+sys1
%Final Impedance Matrix
X=[];
for p=1:49
zeros(p,3);
Zmat= [Z11(p) Z12(p) Z13(p); Z21(p) Z22(p) Z23(p); Z31(p) Z32(p) Z33(p)]
%Admittance Matrix and Eigenvalues
Ymat = inv(Zmat)
Eig = eig(Ymat)
X=[X;{Eig}]
end
real_z1 = real(X)
imag_z1 = imag(X)
figure
plot(real_z1,imag_z1,'g*')

回答(1 个)

madhan ravi
madhan ravi 2020-7-8
[M, ReaL, ImaG] = deal(cell(n, 1); % n number of matrices
for k = 1 : n
M{k} = matrix;
ReaL{k} = real(matrix);
ImaG{k} = imag(matrix);
end
M = cat(1, M{:});
ReaL = cat(1, ReaL{:});
ImaG = cat(1, ImaG{:});

Community Treasure Hunt

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

Start Hunting!

Translated by