Selecting range of data in a matrix

조회 수: 192 (최근 30일)
Francis Chabot
Francis Chabot 2021년 4월 7일
편집: Khalid Mahmood 2021년 4월 7일
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.

채택된 답변

Michael Soskind
Michael Soskind 2021년 4월 7일
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!

추가 답변 (1개)

Khalid Mahmood
Khalid Mahmood 2021년 4월 7일
편집: Khalid Mahmood 2021년 4월 7일
%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
Khalid Mahmood
Khalid Mahmood 2021년 4월 7일
code works fine. I comments of Last 3 lines change A to B

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Discrete Data Plots에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by