How to track changes and match columns in a cell (MATLAB)

1 view (last 30 days)
I have a cell with 3 different columns. The first is a simple ranking, the second is a code composed by X elements and the third is a code composed by Y elements that usually is the same for a certain combination of numbers in column two. So if in column two you have the number 345, it is likely that in column three you will always have 798. The thing is that sometimes it changes. So what I have, for instance, is:
1 453 4789
1 56 229
1 453 1246 %here the corresponding code has changed
2 43 31
2 453 1246 %here the code did not change
3 56 31 %here the corresponding code has changed (it was 229 previously)
What I want to have at the end is a new cell with three columns, only descriminating the cases in which a change in the code of the third column (correspondent to the code form the second column) was observed. For instance, in this simple example I would get:
1 453 1246
3 56 31
Can someone help me? Thanks a lot for your availability

Accepted Answer

Geoff Hayes
Geoff Hayes on 27 Apr 2014
Hi Maria,
When you say cell of 3 different columns do you really mean a matrix with N rows and 3 columns? Or do you mean a cell array with three elements? I think the former since you each element has a ranking and two codes.
If it is just a matrix, say mtx, with N rows and 3 columns, and N is not very large, you could sort the matrix on the second and first columns and then iterate over each row to see if there has been a change in the third column. For example, given your data from above:
>> mtx=[ 1 453 4789
1 56 229
1 453 1246
2 43 31
2 453 1246
3 56 31];
Now sort the data on the second column first and then on the first column since you want to see if there has been a change in the third:
>> mtxsorted = sortrows(mtx,[2 1])
mtxsorted =
2 43 31
1 56 229
3 56 31
1 453 4789
1 453 1246
2 453 1246
It's clear from looking at the data where there have been some changes - at row 3 and at row5. So now all you have to write is the code to iterate over each row and compare the value in the second column at row i say with the value at row i-1 - if they are identical, then compare the values in the third column. If they are identical, then do nothing, else if they are different then record the change.
Geoff

More Answers (0)

Categories

Find more on Structures 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!