필터 지우기
필터 지우기

Averaging matrix values after Interpolation

조회 수: 2 (최근 30일)
Benjamin Cowen
Benjamin Cowen 2017년 7월 19일
답변: Jan 2017년 7월 19일
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
Jan
Jan 2017년 7월 19일
편집: Jan 2017년 7월 19일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Jan
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.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by