How to Add Headings to a list of data
4 ビュー (過去 30 日間)
古いコメントを表示
Here is the code I have.
function[root,ea,iter]=secant(func,delta,xr,es,maxit,varargin)
if nargin<3,error('atleast 3 input arguments required'),end
if nargin<4|isempty(es),es=0.0001;end
if nargin<5|isempty(maxit),maxit=50;end
iter=0;
T=[]
while (1)
xrold=xr;
xr=xr-((delta*xr*func(xr))/(func(xr+(delta*xr))-func(xr)));
iter=iter+1;
if xr~=0, ea=abs((xr-xrold)/xr)*100;end
if ea<=es|iter>=maxit,break,end
funcxr=func(xr);
T=[T;[iter,xr,func(xr),ea]];
end
T
root=xr;
Here is the output.
>> secant(@(x) 6*x^2-13*x-5,10^-6,1.2)
T =
[]
T =
1.0000 9.7428 437.8779 87.6832
2.0000 5.5290 106.5398 76.2143
3.0000 3.5319 23.9303 56.5448
4.0000 2.7174 3.9799 29.9710
5.0000 2.5145 0.2472 8.0718
6.0000 2.5001 0.0012 0.5757
7.0000 2.5000 0.0000 0.0029
ans =
2.5000
I need to have headings on the top of the colmuns and have no clue how to add them. Please help
0 件のコメント
採用された回答
Star Strider
2020 年 9 月 14 日
If you have R2013b or later, use a table. The documentation explains how to specify and add the variable names. Note that if you have R2019b or later, the variable names do not need to be valid MATLAB identifiers.
2 件のコメント
Star Strider
2020 年 9 月 14 日
Tout = array2table(T, 'VariableNames',{'iter','xr','func(xr)','ea'})
produces this reault:
Tout =
7×4 table
iter xr func(xr) ea
____ ______ __________ _________
1 9.7428 437.88 87.683
2 5.529 106.54 76.214
3 3.5319 23.93 56.545
4 2.7174 3.9799 29.971
5 2.5145 0.24716 8.0718
6 2.5001 0.001243 0.57567
7 2.5 3.3171e-08 0.0029246
There are a number of different ways to create tables.
Make appropriate changes to get the result you want.
その他の回答 (1 件)
Jon
2020 年 9 月 14 日
編集済み: Jon
2020 年 9 月 14 日
In general, MATLAB doesn't provide for column or row labels for matrices.
You could put the array into a MATLAB table and label the columns (variable names) there.
Here is a simple example where I turn a 5 by 3 array into a table and label the columns
A = rand(5,3)
Atbl = array2table(A,'VariableNames',{'cat','dog','tortoise'})
You should be able to adapt this to your situation.
For more info on working with tables see https://www.mathworks.com/help/matlab/matlab_prog/create-a-table.html
3 件のコメント
Jon
2020 年 9 月 14 日
First get it all into an array (matrix) as you are doing already. Then when you have all of the data in the array (after your while loop) use the array2table command as I earlier suggested.
Jon
2020 年 9 月 14 日
p.s. It looks like StarStrider and I collided on providing you with similar answers at around the same time. I hadn't seen his posted yet when I sent mine.
参考
カテゴリ
Help Center および File Exchange で Line Plots についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!