Grouping an Array with respect to certain elements

Hello,
I have an (Nx5) matrix. Each row corresponds to a struct of data. Data identifiers reside in the first and the second columns of matrix, columns 1 and 2.
A sample matrix:
% srcPort | destPort | send | receive | time
% -------------------------------------------
% 23 | 9212 | 41 | 55 | 202912.0
% 551 | 10563 | 0 | 0 | 55125.0
% 23 | 9212 | 921 | 1020 | 802912.0
% 98 | 5612 | 39 | 24 | 902912.0
I want to be able to group these data w.r.t columns 1 and 2; later want to plot 'time vs send' of each groupped data. Plotting is OK, but I am stuck on determining way to group those data?

댓글 수: 4

Hi Erdem,
May i know what kind of grouping are you referring here. Can it be bit clear with the sample matrix? Is it to group like 239212, 55110563, and so on?
Erdem Tuna
Erdem Tuna 2020년 3월 15일
편집: Erdem Tuna 2020년 3월 15일
Hello Sriram,
Regarding the example, I want to group all matrix rows containing (srcPort=23, destPort=9121) into an identifiable structure such as matrix. I want to do this grouping with all different (srcPort, destPort) pairs. My ultimate purpose is to plot data belonging (srcPort, destPort) pairs.
Hi Erdem,
As i get you, you have an Nx5 matrix, then you wanted to group the first two columns into a separate matrix. So, if the matrix is A (N x 5), then if you define B = A(:,[1 2]); then you will get all the indentifiers in B (N x 2). Is this the same you intended?
Hello,
Indeed I want to group the rows having the same srcPort and dstPort elements, i.e. having same first and second column values.

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

 채택된 답변

Ameer Hamza
Ameer Hamza 2020년 3월 15일
편집: Ameer Hamza 2020년 3월 15일
Try this
data = [23 9212 41 55 202912.0;
551 10563 0 0 55125.0;
23 9212 921 1020 802912.0;
98 5612 39 24 902912.0];
[~,~,ic] = unique(data(:,[1 2]), 'rows');
seperated_data = arrayfun(@(x) data(ic==x,:), unique(ic), 'UniformOutput', 0);
Each element of cell array have same value in column 1 and 2
seperated_data{1}
seperated_data{2}
seperated_data{3}

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

릴리스

R2019a

질문:

2020년 3월 15일

댓글:

2020년 3월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by