Combine 3 array in one matrix of 3 columns

조회 수: 4 (최근 30일)
Philippe Girard
Philippe Girard 2017년 1월 22일
편집: Stephen23 2017년 1월 22일
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);

답변 (1개)

Walter Roberson
Walter Roberson 2017년 1월 22일
Matrice = [(2:30).', P(:); errabs(:)];
However, 2:30 is only 29 entries not 30, so you are going to have problems.
  댓글 수: 2
Philippe Girard
Philippe Girard 2017년 1월 22일
I don't really understand what you just corrected... And well is there a way to combine them or to solve this problem? I can do whatever as long as it's 3 columns.
Stephen23
Stephen23 2017년 1월 22일
편집: Stephen23 2017년 1월 22일
"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).

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by