How to make multiple data sets an equal length

53 visualizaciones (últimos 30 días)
America
America el 18 de Mzo. de 2017
Comentada: Image Analyst el 8 de Mzo. de 2020
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

Respuestas (3)

Image Analyst
Image Analyst el 18 de Mzo. de 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 comentarios
Connor Rudd
Connor Rudd el 8 de Mzo. de 2020
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 el 8 de Mzo. de 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])

Iniciar sesión para comentar.


Image Analyst
Image Analyst el 19 de Mzo. de 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')

Hildo
Hildo el 18 de Mzo. de 2017
Editada: Hildo el 18 de Mzo. de 2017
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 comentario
America
America el 19 de Mzo. de 2017
Editada: America el 19 de Mzo. de 2017
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?

Iniciar sesión para comentar.

Categorías

Más información sobre Live Scripts and Functions en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by