"fit" function requires Columns, but returns dimensions of arrays being concatenated must be consistent

3 次查看(过去 30 天)
I am trying to use the "fit" function, and when I have my data as row vectors, I am getting this error:
>> FitFinder(Ds,Ts,ABVTab)
Error using fit>iFit
X must be a matrix with one or two columns.
Error in fit (line 116)
[fitobj, goodness, output, convmsg] = iFit( xdatain, ydatain, fittypeobj, ...
Error in FitFinder (line 2)
sf=fit([Temp,Dens],ABV,'poly23');
But when I convert the same x and ys to columns, as suggested on the forum, I get this error:
>> FitFinder(Ds(:),Ts(:),ABVTab)
Error using horzcat
Dimensions of arrays being concatenated are not
consistent.
Error in FitFinder (line 2)
sf=fit([Temp,Dens],ABV,'poly23');
I am not sure where I am going wrong. ABVTab is a 61x41 matrix, Ds is a 1x61 vector, Ts is a 1x41 vector, and FitFinder is simply
function [coeff]= FitFinder(Dens,Temp,ABV)
sf=fit([Temp,Dens],ABV,'poly23');

回答(1 个)

Shivang
Shivang 2023-9-26
Hi Lakshman,
I understand you're running into errors while trying to use the fit function.
Note that when using the fit function in the form,
fitobject = fit([x,y],z,fitType)
the data inputs x, y and z must all be column vectors of the same size.
Your data is not in the column vector form. It is in the grid vector form, where length(x) = n, length(y) = m and size(z) = [m,n]. MATLAB provides an in-built function, prepareSurfaceData, to convert data from the grid vector form to the column vector form.
Instead of calling fit directly on the variables Temp, Dens and ABV, first use the prepareSurfaceData function to transform the data,
[TempOut, DensOut, ABVOut] = prepareSurfaceData(Temp, Dens, ABV)
and then call the fit function on the transformed variables TempOut, DensOut, ABVOut
sf=fit([TempOut,DensOut],ABVOut,'poly23')
You should no longer run into any errors.
Hope this helps.
-Shivang

类别

Help CenterFile Exchange 中查找有关 Get Started with Curve Fitting Toolbox 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by