how to store array

2 Ansichten (letzte 30 Tage)
Ls
Ls am 11 Sep. 2021
Kommentiert: Star Strider am 11 Sep. 2021
I have
for x=1:5
A=[5*x 0;2*x^2 1]
B=[0;2.55]
Y=A\B
end
How can I store 5 different matrices of 2X1 in excel

Akzeptierte Antwort

Star Strider
Star Strider am 11 Sep. 2021
Try something like this —
xv=1:5;
for k = 1:numel(xv)
x = xv(k);
A=[5*x 0;2*x^2 1];
B=[0;2.55];
Y(:,x)=A\B;
end
Y
Y = 2×5
0 0 0 0 0 2.5500 2.5500 2.5500 2.5500 2.5500
writematrix(Y,'YourFileName.xlsx')
Experiment to get the result you want.
.
  2 Kommentare
Ls
Ls am 11 Sep. 2021
It does not work when xv =0:0.1:3!! What should we change in case of that???
Star Strider
Star Strider am 11 Sep. 2021
After I created ‘xv’ I forgot to update the subscript in ‘Y’ (corrected here). My apologies.
xv=0:0.1:3;
for k = 1:numel(xv)
x = xv(k);
A=[5*x 0;2*x^2 1];
B=[0;2.55];
Y(:,k)=A\B;
end
Warning: Matrix is singular to working precision.
Y
Y = 2×31
NaN 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 NaN 2.5500 2.5500 2.5500 2.5500 2.5500 2.5500 2.5500 2.5500 2.5500 2.5500 2.5500 2.5500 2.5500 2.5500 2.5500 2.5500 2.5500 2.5500 2.5500 2.5500 2.5500 2.5500 2.5500 2.5500 2.5500 2.5500 2.5500 2.5500 2.5500
figure
plot(xv, Y, '.-')
grid
Beyond that, it works, however with any element of ‘xv’ being 0, there is going to be a NaN result, because the‘Y’ calculation results in a 0/0 operation, that result (or in a few similar situations) will be NaN.
.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by