Combine 3 array in one matrix of 3 columns
3 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
回答 (1 件)
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 件のコメント
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 Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!