Selecting range of data in a matrix
192 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
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!
0 个评论
更多回答(1 个)
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Discrete Data Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!