how to store array

4 views (last 30 days)
Ls
Ls on 11 Sep 2021
Commented: Star Strider on 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

Accepted Answer

Star Strider
Star Strider on 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 Comments
Ls
Ls on 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 on 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.
.

Sign in to comment.

More Answers (0)

Tags

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!