Averaging matrix values after Interpolation
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Jan
2017-7-19
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!