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!