How to display matrices in cell array in a table?

조회 수: 21 (최근 30일)
JingChong Ning
JingChong Ning 2023년 4월 13일
답변: VBBV 2023년 4월 13일
I have a cell array, and each element of the cell array is a matrix. But when I use cell2table to show the array, the table only shows that it is a 4*1 matrix instead of showing the actual value of the matrix. Is there a way to use cell2table to display the matrix values?
Here is my code, the cell array with matrix is named "x". Currently, I have to use celldisp, but it is not a presentable format.
clear
close all
clc
Q1 = [1 0 0 0; 0 1 0 0; 0 0 0 0; 0 0 0 1];
Q2 = [2 0 0 0; 0 0 0 0; 0 0 1 0; 0 0 0 1];
Q3 = [2 0 0 0; 0 0 0 0; 0 0 1 0; 0 0 0 2];
Q4 = [2 0 0 0; 0 0 0 0; 0 0 2 0; 0 0 0 2];
Q5 = [2 0 0 0; 0 0 0 0; 0 0 2 0; 0 0 0 1];
%{
[u,x] = longdyne(Q1);
cell2table(u,"VariableNames",["u0" "u1" "u2" "u3" "u4" "u5" "u6" "u7" "u8" "u9"])
%cell2table(x,"VariableNames",["x0" "x1" "x2" "x3" "x4" "x5" "x6" "x7" "x8" "x9" "x10"])
celldisp(x)
%}
%
[u,x] = longdyne(Q2);
Q2x = cell2table(u,"VariableNames",["u0" "u1" "u2" "u3" "u4" "u5" "u6" "u7" "u8" "u9"])
%cell2table(x,"VariableNames",["x0" "x1" "x2" "x3" "x4" "x5" "x6" "x7" "x8" "x9" "x10"])
celldisp(x)
%}
%
[u,x] = longdyne(Q3);
Q3x = cell2table(u,"VariableNames",["u0" "u1" "u2" "u3" "u4" "u5" "u6" "u7" "u8" "u9"])
%cell2table(x,"VariableNames",["x0" "x1" "x2" "x3" "x4" "x5" "x6" "x7" "x8" "x9" "x10"])
celldisp(x)
%}
%
[u,x] = longdyne(Q4);
Q4x = cell2table(u,"VariableNames",["u0" "u1" "u2" "u3" "u4" "u5" "u6" "u7" "u8" "u9"])
%cell2table(x,"VariableNames",["x0" "x1" "x2" "x3" "x4" "x5" "x6" "x7" "x8" "x9" "x10"])
celldisp(x)
%}
%
[u,x] = longdyne(Q5);
Q5x = cell2table(u,"VariableNames",["u0" "u1" "u2" "u3" "u4" "u5" "u6" "u7" "u8" "u9"])
%cell2table(x,"VariableNames",["x0" "x1" "x2" "x3" "x4" "x5" "x6" "x7" "x8" "x9" "x10"])
celldisp(x)
%}
function [u,x] = longdyne(QN)
A = [0.994 0.026 0 -32.2; -0.094 0.376 820 0; 0 -0.002 0.332 0; 0 0 1 1];
B = [0;-32.7;-2.08;0];
x = cell(1,11);
x{1,1} = [0;10;0;0];
u = cell(1,10);
R = 0;
P = cell(1,10);
P{1,10} = QN;
F = cell(1,10);
F{1,10} = -1*((R+B.'*P{1,10}*B)^(-1))*B.'*P{1,10}*A;
for i = 9:-1:1
P{1,i}=A.'*P{1,i+1}*A-A.'*P{1,i+1}*B*((R+B.'*P{1,10}*B)^(-1))*B.'*P{1,10}*A+QN;
F{1,i} = -1*((R+B.'*P{1,i+1}*B)^(-1))*B.'*P{1,i+1}*A;
end
%celldisp(F)
for i = 1:10
u{1,i}=F{1,i}*x{1,i};
x{1,i+1}=A*x{1,i}+B*u{1,i};
end
end

답변 (2개)

Walter Roberson
Walter Roberson 2023년 4월 13일
No, there is not. table() objects are not designed for display purposes. In every case in which an entry is multiple lines, table() will only display sizes and datatype.
T = table({1}, {(1:2).'}, {1:2}, {(1:3).'}, {1:3}, {1:20})
T = 1×6 table
Var1 Var2 Var3 Var4 Var5 Var6 _____ ____________ _______ ____________ _________ ______________________________________________________ {[1]} {2×1 double} {[1 2]} {3×1 double} {[1 2 3]} {[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20]}
However, row vectors sometimes have their values displayed.

VBBV
VBBV 2023년 4월 13일
you can use array2table & cell2tmat together to display cell array content present in the table as shown below
clear
close all
clc
Q1 = [1 0 0 0; 0 1 0 0; 0 0 0 0; 0 0 0 1];
Q2 = [2 0 0 0; 0 0 0 0; 0 0 1 0; 0 0 0 1];
Q3 = [2 0 0 0; 0 0 0 0; 0 0 1 0; 0 0 0 2];
Q4 = [2 0 0 0; 0 0 0 0; 0 0 2 0; 0 0 0 2];
Q5 = [2 0 0 0; 0 0 0 0; 0 0 2 0; 0 0 0 1];
[u,x] = longdyne(Q1);
Q1x = array2table(cell2mat(u),"VariableNames",["u0" "u1" "u2" "u3" "u4" "u5" "u6" "u7" "u8" "u9"])
Q1x = 1×10 table
u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 __________ __________ ___________ ___________ ___________ __________ _________ _______ _______ ______ -0.0086574 -0.0042254 -1.6577e-05 -1.8409e-05 -2.8398e-05 0.00025997 -0.014429 0.74315 -38.514 1996.1
array2table(cell2mat(x),"VariableNames",["x0" "x1" "x2" "x3" "x4" "x5" "x6" "x7" "x8" "x9" "x10"])
ans = 4×11 table
x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 __ __________ ___________ __________ __________ ___________ ___________ __________ ________ ________ _______ 0 0.26 0.36356 0.42554 0.48583 0.54421 0.60048 0.65403 0.72361 -0.16517 48.403 10 4.0431 -3.3429e-09 7.303e-07 4.1826e-05 -0.00023159 0.0033941 -0.0054012 0.10023 0 0 0 -0.0019926 4.1016e-05 4.8098e-05 5.4259e-05 7.6997e-05 -0.00051471 0.029835 -1.5358 79.599 -4125.4 0 0 -0.0019926 -0.0019516 -0.0019035 -0.0018492 -0.0017722 -0.0022869 0.027548 -1.5083 78.091
[u,x] = longdyne(Q2);
Q2x = array2table(cell2mat(u),"VariableNames",["u0" "u1" "u2" "u3" "u4" "u5" "u6" "u7" "u8" "u9"])
Q2x = 1×10 table
u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 _________ __________ __________ __________ ___________ _________ _________ __________ __________ __________ -0.011239 -0.0036178 -0.0038486 -0.0014606 -7.9764e-05 0.0011088 0.0011992 0.00083069 0.00073796 0.00053194
array2table(cell2mat(x),"VariableNames",["x0" "x1" "x2" "x3" "x4" "x5" "x6" "x7" "x8" "x9" "x10"])
ans = 4×11 table
x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 __ _________ __________ ___________ __________ ___________ ___________ ___________ ___________ ___________ ___________ 0 0.26 0.36576 0.3696 0.29991 0.20492 0.1208 0.060456 0.021084 0.00069389 -0.0041326 10 4.1275 4.4154 2.0726 0.22173 -1.0395 -1.0383 -0.82385 -0.81235 -0.55322 -0.22547 0 0.0033776 0.00039141 -0.00069586 -0.0013382 -0.00072181 -0.00046695 -0.00057285 -0.00027032 2.1684e-19 0 0 0 0.0033776 0.003769 0.0030731 0.001735 0.0010132 0.00054623 -2.6618e-05 -0.00029694 -0.00029694
[u,x] = longdyne(Q3);
Q3x = array2table(cell2mat(u),"VariableNames",["u0" "u1" "u2" "u3" "u4" "u5" "u6" "u7" "u8" "u9"])
Q3x = 1×10 table
u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 _________ __________ __________ __________ ___________ _________ _________ __________ __________ __________ -0.011239 -0.0036176 -0.0038489 -0.0014603 -7.9871e-05 0.0011084 0.0011984 0.00078852 0.00074987 0.00046138
array2table(cell2mat(x),"VariableNames",["x0" "x1" "x2" "x3" "x4" "x5" "x6" "x7" "x8" "x9" "x10"])
ans = 4×11 table
x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 __ _________ __________ ___________ __________ ___________ ___________ ___________ ___________ ___________ ___________ 0 0.26 0.36576 0.36961 0.29992 0.20495 0.12082 0.060471 0.021096 0.00072802 -0.0050912 10 4.1275 4.4153 2.0721 0.22205 -1.0389 -1.0382 -0.8242 -0.80998 -0.47984 -0.19557 0 0.0033774 0.00039083 -0.00069521 -0.0013375 -0.00072202 -0.00046744 -0.00057148 -0.00018144 -2.1684e-19 1.0842e-19 0 0 0.0033774 0.0037682 0.003073 0.0017355 0.0010135 0.00054606 -2.5421e-05 -0.00020686 -0.00020686
[u,x] = longdyne(Q4);
Q4x = array2table(cell2mat(u),"VariableNames",["u0" "u1" "u2" "u3" "u4" "u5" "u6" "u7" "u8" "u9"])
Q4x = 1×10 table
u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 _________ __________ __________ __________ ___________ _________ _________ __________ __________ _________ -0.011239 -0.0036176 -0.0038488 -0.0014604 -7.9891e-05 0.0011083 0.0011974 0.00076669 0.00075485 0.0004244
array2table(cell2mat(x),"VariableNames",["x0" "x1" "x2" "x3" "x4" "x5" "x6" "x7" "x8" "x9" "x10"])
ans = 4×11 table
x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 __ _________ __________ ___________ __________ ___________ __________ ___________ ___________ ___________ ___________ 0 0.26 0.36576 0.36961 0.29993 0.20495 0.12082 0.060469 0.021093 0.00071971 -0.0056494 10 4.1275 4.4153 2.0721 0.2221 -1.0388 -1.0382 -0.82415 -0.80753 -0.44137 -0.1799 0 0.0033774 0.00039087 -0.00069516 -0.0013375 -0.00072207 -0.0004674 -0.00056939 -0.00013546 0 0 0 0 0.0033774 0.0037683 0.0030731 0.0017356 0.0010135 0.00054612 -2.3267e-05 -0.00015872 -0.00015872
[u,x] = longdyne(Q5);
Q5x = array2table(cell2mat(u),"VariableNames",["u0" "u1" "u2" "u3" "u4" "u5" "u6" "u7" "u8" "u9"])
Q5x = 1×10 table
u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 _________ __________ __________ __________ ___________ _________ _________ __________ __________ __________ -0.011239 -0.0036179 -0.0038486 -0.0014607 -7.9784e-05 0.0011087 0.0011982 0.00078908 0.00074887 0.00046245
array2table(cell2mat(x),"VariableNames",["x0" "x1" "x2" "x3" "x4" "x5" "x6" "x7" "x8" "x9" "x10"])
ans = 4×11 table
x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 __ _________ __________ ___________ __________ ___________ ___________ ___________ ___________ ___________ ___________ 0 0.26 0.36576 0.3696 0.29991 0.20492 0.1208 0.060454 0.021082 0.0007021 -0.0051216 10 4.1275 4.4154 2.0727 0.22178 -1.0395 -1.0383 -0.8238 -0.80923 -0.48094 -0.19602 0 0.0033775 0.00039146 -0.00069581 -0.0013381 -0.00072186 -0.00046691 -0.00057073 -0.00018318 0 1.0842e-19 0 0 0.0033775 0.003769 0.0030732 0.0017351 0.0010132 0.00054629 -2.4434e-05 -0.00020761 -0.00020761
%}
function [u,x] = longdyne(QN)
A = [0.994 0.026 0 -32.2; -0.094 0.376 820 0; 0 -0.002 0.332 0; 0 0 1 1];
B = [0;-32.7;-2.08;0];
x = cell(1,11);
x{1,1} = [0;10;0;0];
u = cell(1,10);
R = 0;
P = cell(1,10);
P{1,10} = QN;
F = cell(1,10);
F{1,10} = -1*((R+B.'*P{1,10}*B)^(-1))*B.'*P{1,10}*A;
for i = 9:-1:1
P{1,i}=A.'*P{1,i+1}*A-A.'*P{1,i+1}*B*((R+B.'*P{1,10}*B)^(-1))*B.'*P{1,10}*A+QN;
F{1,i} = -1*((R+B.'*P{1,i+1}*B)^(-1))*B.'*P{1,i+1}*A;
end
%celldisp(F)
for i = 1:10
u{1,i}=F{1,i}*x{1,i};
x{1,i+1}=A*x{1,i}+B*u{1,i};
end
end
hope this helps

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by