Selecting range of data in a matrix

192 views (last 30 days)
Hello,
I'm trying to create different groups from a matrix of dimensions 2784x1.
Data in the matrix range from 0100:9999. From those data I want to create group that have certain range like this :
A = 0100:0999
B = 1000:1499
Then from that, I would be able to create separate matrix for each of them.
Thank you,
Best regards.

Accepted Answer

Michael Soskind
Michael Soskind on 7 Apr 2021
Hi Francis,
There are a number of ways to approach this problem. One simple method is to think of using logical operators to filter out the data. I show this method below. You should be a bit careful in how you choose to filter the data using logic, as you may or may not want to include particular limit values.
% Creating Sample Data
data = round(100+9900*rand(2784,1));
% Setting the limits for matrices A an B
A_lim = [100,999];
B_lim = [1e3,1499];
% Creating indexed filters for the data array in finding values in the
% range for A_lim and B_lim
filt_A = data >= A_lim(1) & data <= A_lim(2); % Using logic operators
filt_B = data >= B_lim(1) & data <= B_lim(2);
% Note, you should be careful about inclusion of the limit values
% Saving the data into arrays A and B filtered by the range
A = data(filt_A);
B = data(filt_B);
There are certainly other ways of generating the arrays A and B, such as using ismember if you generate particular values that you want to compare to, rather than ranges of values.
Hope that helps!

More Answers (1)

Khalid Mahmood
Khalid Mahmood on 7 Apr 2021
Edited: Khalid Mahmood on 7 Apr 2021
%Generate M 2784x1 matrix of unformly distributed random intergers ranging from 100 to 9999
M=randi([100 9999],2784,1)
rangeA=[100 999]; %least and highest values belonging to A
indA=find(M>=rangeA(1) & M<=rangeA(2)) %find indices of values in range of A
A=M(indA); %Assign corresponding values in M to A
%Apply same procedure to range B
rangeB=[1000 1499]; %least and highest values belonging to B
indB=find(M>=rangeB(1) & M<=rangeB(2)) %find indices of values in range of B
B=M(indB); %Assign corresponding values in M to B
  1 Comment
Khalid Mahmood
Khalid Mahmood on 7 Apr 2021
code works fine. I comments of Last 3 lines change A to B

Sign in to comment.

Categories

Find more on Discrete Data Plots in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!