Selecting range of data in a matrix
조회 수: 46(최근 30일)
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.
Michael Soskind 2021년 4월 7일
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!
Khalid Mahmood 2021년 4월 7일
편집: Khalid Mahmood 2021년 4월 7일
%Generate M 2784x1 matrix of unformly distributed random intergers ranging from 100 to 9999
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