How to store vector of doubles in a single cell in a table?
4 次查看(过去 30 天)
显示 更早的评论
Hi everyone,
I would like to store the results from a cfit object in a table after fitting a model with the fit function, including the type of model, the general form, and the coefficients. The coefficients are returned as a vector of doubles, and I would like to store them in the table. Different models can have different numbers of coefficients.
So one solution I'm pursuing is to put the vector of coefficients all into one cell in the table, with a "table" data type. This is the code I am testing with:
x_data = [1;2;4;5;3;7;8;8];
y_data = [12;18;18;20;24;22;25;28];
model_results_tbl = table('Size',[1,5],...
'VariableNames',{'x_label','y_label','fittype','model_form','model_coeff'},...
'VariableTypes',{'cellstr','cellstr','cellstr','cellstr','table'});
[poly1_result, poly1_gof, poly1_output] = fit(x_data,y_data,'poly1');
model_results_tbl(1,:) = {'Xdata','Ydata','poly1',formula(poly1_result),...
table(coeffvalues(poly1_result))};
The code works, except that the "model_coeff" cell is a 1x0 table with no data in it. Running the "table(coeffvalues(poly1_result))" code by itself gives me the output I expect, though:
>>table(coeffvalues(poly1_result))
ans =
table
Var1
________________
1.4903 13.796
What am I doing wrong?
Or is there a better way to do this?
Thanks.
0 个评论
采纳的回答
Ayush Gupta
2020-9-4
The following workaround can be used to store model coefficients in the table. Refer to the following code:
x_data = [1;2;4;5;3;7;8;8];
y_data = [12;18;18;20;24;22;25;28];
model_results_tbl = table('Size',[1,5],...
'VariableNames',{'x_label','y_label','fittype','model_form','model_coeff'},...
'VariableTypes',{'cellstr','cellstr','cellstr','cellstr','cell'});
[poly1_result, poly1_gof, poly1_output] = fit(x_data,y_data,'poly1');
coeff = coeffvalues(poly1_result);
model_results_tbl(1,:) = {'Xdata','Ydata','poly1',formula(poly1_result), mat2cell(coeff,1)};
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Gaussian Process Regression 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!