Selecting range of data in a matrix

171 ビュー (過去 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 Center および File ExchangeLighting, Transparency, and Shading についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by