MATLAB 矩阵格式不正确

发布于 2024-09-28 05:54:12 字数 1254 浏览 2 评论 0原文

我下面有一些代码,但我似乎无法正确格式化矩阵。我一直在尝试使用 \t 和 fprintf 让矩阵看起来更专业(紧密结合在一起),但似乎无法这样做。我在为矩阵的每一列添加标题时也遇到一些麻烦。任何帮助将不胜感激!

clear all
clc

format('bank')

%    input file values    %
A = [4 6 5 1 0 0 0 0 0; 7 8 4 0 1 0 0 0 0; 6 5 9 0 0 1 0 0 0; 1 0 0 0 0 0 -1 0 0; 0 1 0 0 0 0 0 -1 0; 0 0 1 0 0 0 0 0 -1];
b = [480; 600; 480; 24; 20; 25];
c = [3000 4000 4000 0 0 0 0 0 0];

% Starting xb %
xb = [1 2 3 4 5 6]

% Starting xn %
xn = [7 8 9]


cb = c(xb)
cn = c(xn)

% Get B from A %
B = A(:,xb)

% Get N from A %
N = A(:,xn)

% Calculate z %
z = ((cb*(inv(B))*A)-c)

% Calculate B^(-1) %
Binv = inv(B)

% Calculate RHS of row 0 %
RHS0 = cb*Binv*b

% Calculates A %
A = Binv*A


%STARTING Tableau%
ST = [z RHS0;A b]
for j=1:A
    fprintf(1,'\tz%d',j)
end

q = 0
while q == 0



    m = input('what is the index value of the ENTERING variable?  ')
    n = input('what is the index value of the LEAVING variable?  ')

    xn(xn==m)= n
    xb(xb==n) = m


    cb = c(xb)
    cn = c(xn)
    B = A(:,xb)
    N = A(:,xn)

    Tableuz = (c-(cb*(B^(-1))*A))
    RHS0 = (cb*(B^(-1))*b)
    TableuA = ((B^(-1))*A)
    Tableub = ((B^(-1))*b)

    CT = [Tableuz RHS0; TableuA Tableub];
    disp(CT)
    q = input('Is the tableau optimal? Y-1, N-0')


end

I have some code below, and I cant seem to get the matrices formatted correctly. I have been trying to get the matrices to look more professional (close together) with \t and fprintf, but cant seem to do so. I am also having some trouble putting titles for each columns of the matrix. Any help would be much appreciated!

clear all
clc

format('bank')

%    input file values    %
A = [4 6 5 1 0 0 0 0 0; 7 8 4 0 1 0 0 0 0; 6 5 9 0 0 1 0 0 0; 1 0 0 0 0 0 -1 0 0; 0 1 0 0 0 0 0 -1 0; 0 0 1 0 0 0 0 0 -1];
b = [480; 600; 480; 24; 20; 25];
c = [3000 4000 4000 0 0 0 0 0 0];

% Starting xb %
xb = [1 2 3 4 5 6]

% Starting xn %
xn = [7 8 9]


cb = c(xb)
cn = c(xn)

% Get B from A %
B = A(:,xb)

% Get N from A %
N = A(:,xn)

% Calculate z %
z = ((cb*(inv(B))*A)-c)

% Calculate B^(-1) %
Binv = inv(B)

% Calculate RHS of row 0 %
RHS0 = cb*Binv*b

% Calculates A %
A = Binv*A


%STARTING Tableau%
ST = [z RHS0;A b]
for j=1:A
    fprintf(1,'\tz%d',j)
end

q = 0
while q == 0



    m = input('what is the index value of the ENTERING variable?  ')
    n = input('what is the index value of the LEAVING variable?  ')

    xn(xn==m)= n
    xb(xb==n) = m


    cb = c(xb)
    cn = c(xn)
    B = A(:,xb)
    N = A(:,xn)

    Tableuz = (c-(cb*(B^(-1))*A))
    RHS0 = (cb*(B^(-1))*b)
    TableuA = ((B^(-1))*A)
    Tableub = ((B^(-1))*b)

    CT = [Tableuz RHS0; TableuA Tableub];
    disp(CT)
    q = input('Is the tableau optimal? Y-1, N-0')


end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

み零 2024-10-05 05:54:12

我没有深入研究你在做什么,但有几点建议:
* 在您不想打印到屏幕上的行末尾添加分号——这样可以更轻松地查看其他地方发生的情况。
* 你的 for j=1: 循环只打印 j。我认为你想要的更像是这样的:

for row = 1:size(A,1)
   for column = 1:size(A,2)
       fprintf('%10.2f', A(row,column));
    end
    fprintf('\n');
end
  • 如果你还没有使用过Matlab调试器,请尝试一下;它使许多此类问题更容易被发现。要启动它,您所要做的就是通过单击行号旁边的破折号(-)并启动脚本来向文件添加断点。快速的网络搜索也可以很快找到解决方案——其他人通常已经遇到了您将遇到的任何问题。

祝你好运。

I didn't dig into what you are doing really deeply, but a few pointers:
* Put semicolons at the end of lines you don't want printing to the screen--it makes it easier to see what is happening elsewhere.
* Your for j=1:A loop only prints j. I think what you want is more like this:

for row = 1:size(A,1)
   for column = 1:size(A,2)
       fprintf('%10.2f', A(row,column));
    end
    fprintf('\n');
end
  • If you haven't used the Matlab debugger yet, give it a try; it makes a lot of these problems easier to spot. All you have to do to start it is to add a breakpoint to the file by clicking on the dash(-) next to the line numbers and starting the script. Quick web searches can turn up the solution very quickly too--someone else has usually already had any problem you're going to run into.

Good luck.

花辞树 2024-10-05 05:54:12

尝试使用 num2str ,格式参数为您想要的精度。它用于将矩阵转换为字符串。 (注意:这与 mat2str 不同,后者序列化矩阵,以便可以使用 eval 反序列化矩阵)

Try using num2str with a format argument of your desired precision. It's meant for converting matrices to strings. (note: this is different than mat2str which serializes matrices so they can be deserialized with eval)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文