Combine 3 array in one matrix of 3 columns
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
How can I combine these columns? I'm running this short program here:
clc
clear all
P(2)=2*sqrt(2);
for n=2:30
P(n+1)=2^n*sqrt(2*(1-sqrt(1-(P(n)/2^n)^2)));
errabs(n)=abs(pi-P(n));
end
P(31)=[];
Matrice=[2:30;P;errabs];
fprintf('%s %13s %31s\t \n','n','Pn','Erreur absolue');
But even if in the workspace I can see that they're all 30 columns long I can't concatenate them.
Can anyone help me on this one?
I ran this one this morning without any problems:
clc
fprintf('a)\n');
v=0;
for i=1:13
v(i)=factorial(i);
end
fprintf('v=\t')
fprintf('%d\t',v);
%partie b)
fprintf('\n\nb)\n');
s_n=0;
for i=1:13
s_n(i)=sqrt(2*pi*i)*(i/exp(1))^i;
end
fprintf('Lapproximation est de:\n');
fprintf('%f\t',s_n);
%c)
for i=1:13
errabs(i)=abs(v(i)-s_n(i));
errrel(i)=abs((v(i)-s_n(i))./v(i));
end
fprintf('\n\nb)\nLerreur absolue est de:\n');
fprintf('%f\t',errabs);
fprintf('\n');
fprintf('\nc)\nLerreur relative est de:\n');
fprintf('%f\t',errrel);
disp(' ');
disp(' ');
disp('d)');
Matrice=[1:13;errabs;errrel];
fprintf('\n%s %20s\t\t %20s','n','Erreur absolue','Erreur relative');
fprintf('\n %d\t %1.16e \t %1.16e\n\n \n',Matrice);
0 comentarios
Respuestas (1)
Walter Roberson
el 22 de En. de 2017
Matrice = [(2:30).', P(:); errabs(:)];
However, 2:30 is only 29 entries not 30, so you are going to have problems.
2 comentarios
Stephen23
el 22 de En. de 2017
Editada: Stephen23
el 22 de En. de 2017
"is there a way to combine them or to solve this problem"
Yes, using the (:) syntax that Walter Roberson showed you. Walter Roberson also told you why this will fail when you use it (because you have one column that is shorter than the others).
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!