How to categorize data in a matrix?
Show older comments
For example, I have a 100 x 3 matrix A
A = [1 0 542
1 5 21
1 10 125
1 15 6245
1 20 2678
1 50 580
1 55 1205
1 60 196
2 0 5129
2 5 34
2 10 248
2 15 348
2 20 583
2 25 480
… … ...]
The first column is the ID of an object, where I am to identify the number of times it moved determined by the second column. Movement is determined by the spacing of the values in column 2, if the spacing is more than 5 it will be categorized as a new move. I want to determine how many moves each object has done. Also, the data in the 3rd column needs to be recorded at the start and end of a move.
Edit: Movement is determined by the spacing of the values in column 2, not row 2
2 Comments
Image Analyst
on 5 Dec 2020
"Movement is determined by the spacing of the values in row 2" Why row 2? Row 2 is [1,5,21] -- what is the "spacing" of those 3 values? Is it 4 and 16?
And for column 3, you have a single number in column 2. Like for row 2 it's 21. When you say "the data in the 3rd column needs to be recorded at the start and end of a move" where exactly is this recording to be logged? In a new matrix that has two columns with one column being the starting point and one column being the ending point? How do we know the starting point?
And you say column 2 is the number of times a certain ID number has moved. But each ID has several numbers for the number of moves it made. So is each row like a time point and the column 2 is the number of moves that ID number has made by that point in time? Or were those independent experiments, like sometimes #1 moved 5 moves and on another run it moved 20 times, and on another run/experiment it moved 10 times?
Joaquin Lalusin
on 5 Dec 2020
Answers (1)
KSSV
on 5 Dec 2020
A = [1 0 542
1 5 21
1 10 125
1 15 6245
1 20 2678
1 50 580
1 55 1205
1 60 196
2 0 5129
2 5 34
2 10 248
2 15 348
2 20 583
2 25 480] ;
id = A(:,1) ;
steps = A(:,2) ;
[c,ia,ib] = unique(id) ;
N = length(c) ; % number of id's present
iwant = cell(N,1) ;
steps_count = zeros(N,1) ;
for i = 1:N
iwant{i} = A(ib==i,:) ;
T = length(unique(steps(ib==i))) ;
steps_count(i) = T ;
fprintf("%d id has %d number of steps\n",i,T) ;
end
iwant
steps_count
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!