I have data from four different tests and I want to get an average

조회 수: 1 (최근 30일)
I have load vs. deflection data of 4 different tests and I want to get an average out of it but all data has different number of rows. How do I make them equal and get an average Load-deflection plot?
clc;
clear;
Data=xlsread('LoadDeflData.xlsx');
x1= Data(:,2); x2= Data(:,4); x3=Data(:,6);x4=Data(:,8);
y1=Data(:,1);y2=Data(:,3);y3=Data(:,5);y4=Data(:,7);
figure(1)
plot(x1,y1); hold on;
plot(x2,y2); hold on;
plot(x3,y3); hold on;
plot(x4,y4); hold on;

채택된 답변

Image Analyst
Image Analyst 2020년 9월 29일
Try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 22;
filename = 'LoadDeflData.xls';
if ~isfile(filename)
errorMessage = sprintf('File not found:\n%s', filename)
uiwait(errordlg(errorMessage));
return;
end
% Data = xlsread(filename);
Data = readmatrix(filename);
x1 = Data(:,2); x2 = Data(:,4); x3 = Data(:,6); x4 = Data(:,8);
y1 = Data(:,1); y2 = Data(:,3); y3 = Data(:,5); y4 = Data(:,7);
%---------------------------------------------------------------------
% Get rid of nans:
badIndexes = isnan(x1);
x1(badIndexes) = [];
y1(badIndexes) = [];
% Get rid of nans:
badIndexes = isnan(x2);
x2(badIndexes) = [];
y2(badIndexes) = [];
% Get rid of nans:
badIndexes = isnan(x3);
x3(badIndexes) = [];
y3(badIndexes) = [];
% Get rid of nans:
badIndexes = isnan(x4);
x4(badIndexes) = [];
y4(badIndexes) = [];
%---------------------------------------------------------------------
% The next problem: many of the x values are duplicates and that causes problems with interpolation.
% So we need to get the average y for each duplciated x
% First get group means for set #1:
groups = findgroups(x1);
x1 = splitapply(@mean, x1, groups);
y1 = splitapply(@mean, y1, groups);
% First get group means for set #2:
groups = findgroups(x2);
x2 = splitapply(@mean, x2, groups);
y2 = splitapply(@mean, y2, groups);
% First get group means for set #3:
groups = findgroups(x3);
x3 = splitapply(@mean, x3, groups);
y3 = splitapply(@mean, y3, groups);
% First get group means for set #4:
groups = findgroups(x4);
x4 = splitapply(@mean, x4, groups);
y4 = splitapply(@mean, y4, groups);
%---------------------------------------------------------------------
% Plot repaired curves.
plot(x1, y1, '-', 'LineWidth', 2);
grid on;
hold on;
plot(x2, y2, '-', 'LineWidth', 2);
plot(x3, y3, '-', 'LineWidth', 2);
plot(x4, y4, '-', 'LineWidth', 2);
grid on;
legend('y1', 'y2', 'y3', 'y4');
% Maximize figure window.
g = gcf;
g.WindowState = 'maximized';
%---------------------------------------------------------------------
% Get a list of all the x's and get the unique values from the entire list.
x = unique([x1; x2; x3; x4]);
%---------------------------------------------------------------------
% Interpolate y values.
% Get new y with values at every x value possible.
y1a = interp1(x1, y1, x);
y2a = interp1(x2, y2, x);
y3a = interp1(x3, y3, x);
y4a = interp1(x4, y4, x);
%---------------------------------------------------------------------
% Compute the average at each of those x values.
averageY = (y1a + y2a + y3a + y4a) / 4;
plot(x, averageY, 'k-', 'LineWidth', 7);
legend('y1', 'y2', 'y3', 'y4', 'average y');
This gives values for where the x is there for all curves. If you want to continue to take the average where there are fewer curves, like beyond 0.3 where there are only 3 curves instead of 4, you'll have to add a few lines of code. If this does the trick, please "Accept this answer". Thanks in advance.

추가 답변 (1개)

Image Analyst
Image Analyst 2020년 9월 27일
You blew by the posting guidelines and forgot to attach any data!
Do you have unique x and y values for each? Then I'd get a common set of x like
x = unique([x1, x2, x3, x4]));
% Get new y with values at every x value possible.
y1a = interp1(x1, y1, x);
y2a = interp1(x2, y2, x);
y3a = interp1(x3, y3, x);
y4a = interp1(x4, y4, x);
% Compute the average at each of those x values.
averageY = (y1a+y2a+y3a+y4a)/4;
  댓글 수: 3
Image Analyst
Image Analyst 2020년 9월 28일
편집: Image Analyst 2020년 9월 28일
Please give me code to get the x and y. I see only 3 possible vectors in there, not 4. Also I'm not sure I'm getting them out right because this is what I got:
filename = 'LoadDeflData.xls'
data = readmatrix(filename)
x1 = data(:, 1);
y1 = data(:, 2);
% Get rid of nans:
badIndexes = isnan(x1);
x1(badIndexes) = [];
y1(badIndexes) = [];
plot(x1, y1, '-', 'LineWidth', 2);
grid on;
hold on;
x2 = data(:, 3);
y2 = data(:, 4);
% Get rid of nans:
badIndexes = isnan(x2);
x2(badIndexes) = [];
y2(badIndexes) = [];
plot(x2, y2, '-', 'LineWidth', 2);
x3 = data(:, 5);
y3 = data(:, 6);
% Get rid of nans:
badIndexes = isnan(x3);
x3(badIndexes) = [];
y3(badIndexes) = [];
plot(x3, y3, '-', 'LineWidth', 2);
% Get a list of all the x's.
x = unique([x1; x2; x3]);
% Get new y with values at every x value possible.
y1a = interp1(x1, y1, x);
y2a = interp1(x2, y2, x);
y3a = interp1(x3, y3, x);
% Compute the average at each of those x values.
averageY = (y1a+y2a+y3a)/3;
plot(x, averageY, '-', 'LineWidth', 2);
legend('y1', 'y2', 'y3', 'average y');
It looks like the curves have 2 y values for some x values.
Devansh Deepak Patel
Devansh Deepak Patel 2020년 9월 28일
편집: Image Analyst 2020년 9월 29일
Thank you for your patience. I have this .xlsx file now and with this code I get this plot. I want to get the average of this 4 curves
clc;clear;
Data=xlsread('LoadDeflData.xlsx');
x1= Data(:,2); x2= Data(:,4); x3=Data(:,6);x4=Data(:,8);
y1=Data(:,1);y2=Data(:,3);y3=Data(:,5);y4=Data(:,7);
figure(1)
plot(x1,y1); hold on;
plot(x2,y2); hold on;
plot(x3,y3); hold on;
plot(x4,y4); hold on;

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

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by