# MATLAB matrix not formatting correctly – Education Career Blog

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
``````

,

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.

,

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`)