0
votes

I have a cell array of data. The array is a single column with something between 500 and 3000 elements. Each element consists of a matrix with four columns. The first column are the x-, the second the y-values, whereas the third, and fourth column are irrelevant.

What I would like to do know is to fit these x- and y-values to a linear function (y=a*x+b), but not all the values, only a fraction - e.g. the first 10, 20, or 50%, the rest should not be considered. I have problems to access the relevant data in the cell array properly and to find a way to fit only a fraction of the data. What makes the task for me even more challenging is that the number of x- and y-values is different for every element of the array.

Although the elements of the array all have the size 500x4, some of them only exhibit several x- and y-paris and almost only NaN at the end, some of them almost 500 and only several NaN.

1
Why don't you first put your X,Y data into a single matrix, and then go from there? You may find it easier.Micah Smith

1 Answers

0
votes
% cell array of data...
% single column, 500 to 3000 elements..
mycell = cell(1000,1);


for k = 1 : 1000

    % each element is a matrix with 4 columns.. 500x4
    mycell{k} = rand(500,4);

end

% first 10% means 500/10 or 50 elements
% first 50% means 500/2 or 250 elements

% lets fit the first 10% (50 elements)

for k = 1 : 1000

    % get elements 1:50, or 10%

    % x is col 1
    x = mycell{k}(1:50, 1);

    % y is col 2
    y = mycell{k}(1:50, 2);


    %  will leave it to you to figure out the fitting. see:
    %   http://www.mathworks.com/help/matlab/data_analysis/linear-regression.html    

    polyfit(x, y, 1);


end