How to make multiple data sets an equal length
11 次查看(过去 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
0 个评论
回答(3 个)
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
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
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
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')

0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Performance 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!