How do I sample a data set and then combine into an array to display three different variable?

2 visualizaciones (últimos 30 días)
2.5. Draw a random sample (without replacement) of 10 people with BMI between 23 and 25, and display their heights and weights. Seed your random number generator with ‘rng(912)’. It is from Data = load(‘Weight.txt’), height = data[:,1], weight = data[:,2], weight = data[:,3]. I am super new to MatLab and have never used it or been in a statistics class before. An explanation of how to do it would be much appreciated thanks or any guidance for that matter.

Respuestas (2)

Sahas
Sahas el 11 de Sept. de 2024
Hi @John,
As per my understanding, you want help in selecting 10 people randomly without replacement whose BMI is between 23 and 25 and display their heights and weights.
Follow the steps listed below to accomplish the task. Refer to the MathWorks documentation links provided for more information on the usage of those MATLAB functions:
  1. Seed the random number generator using MATLAB’s “rng” function: https://www.mathworks.com/help/matlab/ref/rng.html
  2. Load the “Weights.txt” file and extract the heights and weights: https://www.mathworks.com/help/matlab/ref/load.html
  3. Make a vector for BMI and calculate BMI of each individual using the height and weight vectors.
  4. Find indices of individuals with BMI between 23 and 25: https://www.mathworks.com/help/matlab/ref/find.html
  5. Randomly select 10 individuals without replacement: https://www.mathworks.com/help/stats/randsample.html
  6. Use the previously identified indices to select values from the height and weight arrays and form the resulting vector: https://www.mathworks.com/help/matlab/math/array-indexing.html
  7. Display the vectors: https://www.mathworks.com/help/matlab/ref/disp.html
I hope this is beneficial and helps you get started with the task!

Sachin Lodhi
Sachin Lodhi el 11 de Sept. de 2024
Hi John, to achieve your goal of drawing a random sample of 10 people with a BMI between 23 and 25 from your dataset and display their heights and weights, you can follow these steps -
  1. Load the Data: First, you need to load the data from the 'Weight.txt' file. Given your description, it seems like the file contains three columns: an 'ID' or 'index', 'height', and 'weight'.
  2. Calculate BMI: Calculate the BMI (Body Mass Index) using the standard formulae. Make sure to convert height from centimeters to meters if necessary.
  3. Filter Data: Identify individuals with a BMI between 23 and 25.
  4. Random Sampling: Use the 'rng' function to set the seed for reproducibility and then randomly sample 10 individuals without replacement.
  5. Display Results: Show the heights and weights of the sampled individuals.
Here's the MATLAB code implementing these steps:
% Step 1: Load the data
Data = load('Weight.txt');
height = Data(:, 2); % Assuming height is in the second column
weight = Data(:, 3); % Assuming weight is in the third column
% Step 2: Calculate BMI
% Convert height to meters if it's in centimeters
height_m = height / 100;
BMI = weight ./ (height_m .^ 2);
% Step 3: Filter data for BMI between 23 and 25
valid_indices = find(BMI >= 23 & BMI <= 25);
% Step 4: Random Sampling
rng(912); % Set seed for reproducibility
sample_indices = randsample(valid_indices, 10);
% Step 5: Display Results
sample_heights = height(sample_indices);
sample_weights = weight(sample_indices);
disp('Sampled Heights and Weights:');
disp(table(sample_heights, sample_weights, 'VariableNames', {'Height', 'Weight'}));
I hope this helps!

Community Treasure Hunt

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

Start Hunting!

Translated by