- Seed the random number generator using MATLAB’s “rng” function: https://www.mathworks.com/help/matlab/ref/rng.html
- Load the “Weights.txt” file and extract the heights and weights: https://www.mathworks.com/help/matlab/ref/load.html
- Make a vector for BMI and calculate BMI of each individual using the height and weight vectors.
- Find indices of individuals with BMI between 23 and 25: https://www.mathworks.com/help/matlab/ref/find.html
- Randomly select 10 individuals without replacement: https://www.mathworks.com/help/stats/randsample.html
- 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
- Display the vectors: https://www.mathworks.com/help/matlab/ref/disp.html
How do I sample a data set and then combine into an array to display three different variable?
2 views (last 30 days)
Show older comments
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.
0 Comments
Answers (2)
Sahas
on 11 Sep 2024
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:
I hope this is beneficial and helps you get started with the task!
0 Comments
Sachin Lodhi
on 11 Sep 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 -
- 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'.
- Calculate BMI: Calculate the BMI (Body Mass Index) using the standard formulae. Make sure to convert height from centimeters to meters if necessary.
- Filter Data: Identify individuals with a BMI between 23 and 25.
- Random Sampling: Use the 'rng' function to set the seed for reproducibility and then randomly sample 10 individuals without replacement.
- 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!
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!