How to make multiple data sets an equal length
31 views (last 30 days)
Show older comments
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
0 Comments
Answers (3)
Image Analyst
on 18 Mar 2017
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 Comments
Connor Rudd
on 8 Mar 2020
I know it has been a while, but doing this just adds NaN to any of the smaller sets of data
Image Analyst
on 8 Mar 2020
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
on 19 Mar 2017
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')
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/173434/image.png)
0 Comments
See Also
Categories
Find more on Live Scripts and Functions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!