how to change disp for fprintf

1 次查看(过去 30 天)
Emanuel Barrios
Emanuel Barrios 2021-4-28
编辑: Adam Danz 2021-4-28
%Algoritmo sist_ecs_lineales
n=input("Proporciona el número de incógnitas del sistema");
Au=input("Ingresa la matriz aumentada del sistema");
disp(Au)
[num_filas,num_columnas]=size(Au); %n Es el número de incognitas del sistema
disp("Primer paso de la Eliminación de Gauss (ESTRATEGIA DE PIVETEO NATURAL)")
disp("a)Elección del pivote")
pivote=Au(1,1)
disp("b) Poner ceros debajo de la columna de donde se eligio el pivote")
%Calcula los multiplicadores (escalares)
lambda1=Au(2,1)/pivote;
lambda2=Au(3,1)/pivote;
Au(2,:)=-lambda1*Au(1,:)+Au(2,:)
Au(3,:)=lambda2*Au(1,:)+Au(3,:)
disp("Primer paso de la Eliminación de Gauss (ESTRATEGIA DE PIVETEO NATURAL)")
disp("a)Elección del pivote")
pivote=Au(1,1)
disp("b) Poner ceros debajo de la columna de donde se eligio el pivote")
%Calcula los multiplicadores (escalares)
lambda1=Au(3,2)/pivote;
Au(3,:)=-lambda1*Au(2,:)+Au(3,:)
disp("SUSTITUCIÓN HACIA ATRAS")
%Fin algoritmo
  1 个评论
Adam Danz
Adam Danz 2021-4-28
编辑:Adam Danz 2021-4-28
We need to know what the expected inputs are to be displayed. Rather than needing to run your code, how about you provide some more specific examples of what is being displayed and how you would like it to appear using fprintf.

请先登录,再进行评论。

回答(1 个)

Adam Danz
Adam Danz 2021-4-28
编辑:Adam Danz 2021-4-28
Starting in Matlab r2021a, you can use formattedDisplayText() to capture anything that is displayed by disp().
See a summary of this new feature in the Community Highlights.
If you want to use fprintf instead, there are several examples in the documentation but when it comes to displaying multidimensional arrays such as matrices, it gets tricky. See the example provided by Jan.
You could also use string or num2str for numeric conversion to strings or character arrays.
Examples
% Example 1
a = randi(9,3,3)
a = 3×3
9 3 5 1 2 1 8 5 9
formattedDisplayText(a)
ans =
" 9 3 5 1 2 1 8 5 9 "
string(a)
ans = 3×3 string array
"9" "3" "5" "1" "2" "1" "8" "5" "9"
num2str(a)
ans = 3×7 char array
'9 3 5' '1 2 1' '8 5 9'
% Example 2
b = {'a', 7; 'b', 12}
b = 2×2 cell array
{'a'} {[ 7]} {'b'} {[12]}
string(b)
ans = 2×2 string array
"a" "7" "b" "12"
formattedDisplayText(b)
ans =
" {'a'} {[ 7]} {'b'} {[12]} "

类别

Help CenterFile Exchange 中查找有关 Card games 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by