Averaging matrix values after Interpolation

3 views (last 30 days)
Benjamin Cowen
Benjamin Cowen on 19 Jul 2017
Answered: Jan on 19 Jul 2017
Hey, I have a matrix that is 1x24. Within each cell is another matrix that is a single column. However, the column length changes from one matrix to another. Is there a way to average the first row from each matrix, and output into a new matrix where the first cell is the average of all the matrix first cells? Then second and third all the way to the end?
Here is my script so far. You can see that I create a matrix. Now I would like to average them (after I interpolate them).
close all
clear
clc
k = cell(1,2);
for k=1:24
data{k} = xlsread('C:\data.xlsx',['PKA', num2str(k)]);
end
for i=1:24
xfinal{i}=data{1,i}(end,1);
xi{i}=0:0.001:xfinal{i};
xi{i}=transpose(xi{i});
x{i}=data{1,i}(:,1);
y{i}=data{1,i}(:,4);
yi{i} = interp1(x{i},y{i},xi{i});
end
  1 Comment
Jan
Jan on 19 Jul 2017
Edited: Jan on 19 Jul 2017
You mean:
data = cell(1,24); % Not k = cell(1,2)
"k" is overwritten in the next line by the counter of the for loop.
You mix the terms "cell" and "matrix" randomly. Better use "cell matrix" and "cell element" if you talk about cell objects only.

Sign in to comment.

Answers (1)

Jan
Jan on 19 Jul 2017
Which is the cell you want to average over? If it is yi:
v = zeros(1,24);
for k = 1:24
v(k) = yi{k}(1);
end
meanV = mean(v);
Now it matters what "Then second and third all the way to the end" exactly means, because the vectors have a different size. What is "end" then?
What about joining the vectors to a matrix and padding it with NaNs:
Len = cellfun('length', yi);
V = NaN(max(Len), 24);
for iC = 1:24
V(1:Len(iC), iC) = yi{iC};
end
Now you can uses nanmean or mean(V, 1, 'omitnan') to calculate the average.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!