How to make multiple data sets an equal length

51 次查看(过去 30 天)
I have multiple data sets that I would like to merge and plot but am having a bit of difficulty because they are all different lengths. For example, I am measuring thicknesses of bone around a joint from 1-100% but one specimen has 1185 entries while another has 1406 entries.
Therefore, how would I be able to make all of these data sets an equal length so that they can be scaled and plotted correctly?
Thank you, America

回答(3 个)

Image Analyst
Image Analyst 2017-3-18
You can use interp1(). Let's say maxLength is the maximum number of elements in any of your vectors of thicknesses. Then
maxLength = max([length(thickness1), length(thickness2), length(thickness3)]);
xFit = 1:maxLength;
interpThickness1 = interp1(1:length(thickness1), thickness1, xFit);
interpThickness2 = interp1(1:length(thickness2), thickness2, xFit);
interpThickness3 = interp1(1:length(thickness3), thickness3, xFit);
and so on.
  2 个评论
Connor Rudd
Connor Rudd 2020-3-8
I know it has been a while, but doing this just adds NaN to any of the smaller sets of data
Image Analyst
Image Analyst 2020-3-8
You're right. Not sure what I was thinking. Try this:
thickness1 = randi(9, 1, 10)
thickness2 = randi(9, 1, 12)
thickness3 = randi(9, 1, 16)
maxLength = max([length(thickness1), length(thickness2), length(thickness3)]);
interpThickness1 = imresize(thickness1, [1, maxLength])
interpThickness2 = imresize(thickness2, [1, maxLength])
interpThickness3 = imresize(thickness3, [1, maxLength])

请先登录,再进行评论。


Image Analyst
Image Analyst 2017-3-19
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 = 25;
% Open data
filenameGorilla = 'Sub.Th_Gorilla_20039.csv';
filenameHuman = 'Sub.Th_Human_39.csv';
xyGorilla = csvread(filenameGorilla, 1);
xyHuman = csvread(filenameHuman, 1);
% Extract x and y data
xGorilla = xyGorilla(:, 1);
yGorilla = xyGorilla(:, 2);
xHuman = xyHuman(:, 1);
yHuman = xyHuman(:, 2);
% NOTE: THERE ARE SOME NANS IN THE DATA.
% If you want to remove those, use isnan():
badRows = isnan(yGorilla); % Logical vector.
xGorilla = xGorilla(~badRows); % Extract good rows.
yGorilla(badRows) = []; % Alternate way to remove, set bad rows equal to null.
badRows = isnan(yHuman); % Logical vector.
xHuman = xHuman(~badRows); % Extract good rows.
yHuman(badRows) = []; % Alternate way to remove, set bad rows equal to null.
% Interpolate human y data so that it's estimated
% at the locations of the Gorilla x locations:
yHumanInterpolated = interp1(xHuman, yHuman, xGorilla);
% Plot them both at the xGorilla locations.
plot(xGorilla, yGorilla, 'k.-', 'LineWidth', 2, 'MarkerSize', 20);
hold on;
% Plot fitted human.
plot(xGorilla, yHumanInterpolated, 'r.-', 'LineWidth', 2, 'MarkerSize', 20);
% OPTIONAL -- Plot original human raw data.
plot(xHuman, yHuman, 'm.', 'LineWidth', 2, 'MarkerSize', 10);
% Label the graph.
title('Gorilla vs. Human', 'FontSize', fontSize);
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
grid on;
legend('Gorilla', 'Human Fit', 'Human Raw', 'Location', 'north');
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')

Hildo
Hildo 2017-3-18
编辑:Hildo 2017-3-18
Can you clarify or post some data example?
I think you just have to create the correct "X data" to plot.
%length(Y1)
%100
%length(Y2)
%200
plot(1:length(Y1),Y1,1:length(Y2),Y2)
  1 个评论
America
America 2017-3-19
编辑:America 2017-3-19
Sure. The data are all in .csv files. Here is an example of two of the .csv files. How would I be able to import the .csv files into the code?
Column A is the percentages from 1-100% and column B is the thickness values at that point. The only problem being that each specimen has a different number of entries/rows.
Does that make sense?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Live Scripts and Functions 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by