I know this has been asked before, but im just not sure how to approach it.
I have a cell that looks
bbox
[0,241,637,168]
[204,181,382,286]
[56,314,185,243]
[0,59,574,506]
[8,58,230,546]
and another "key" cell that looks like this
1
1
2
2
3
I also have a cell that called "class"
5
11
12
9
10
I need an output like this
row bbox class
1 [0,241,637,168; 204,181,382,286] [5;11]
2 [56,314,185,243; 0,59,574,506] [12;9]
3 [8,58,230,546] [10]
Any info would help, I am sure that it uses either the unique() or accumarray() function

 Réponse acceptée

Jan
Jan le 11 Nov 2022
Modifié(e) : Jan le 11 Nov 2022
bbox = {[0,241,637,168]; ...
[204,181,382,286]; ...
[56,314,185,243]; ...
[0,59,574,506]; ...
[8,58,230,546]};
key = [1, 1, 2, 2, 3];
result = splitapply(@(c) {cat(1, c{:})}, bbox, key(:))
result = 3×1 cell array
{2×4 double } {2×4 double } {[8 58 230 546]}
Another option is a simple loop:
ukey = unique(key);
result = cell(numel(ukey), 1);
for k = 1:numel(ukey)
result{k} = cat(1, bbox{key == ukey(k)});
end
result
result = 1×3 cell array
{2×4 double} {2×4 double} {[8 58 230 546]}

5 commentaires

Conner Carriere
Conner Carriere le 11 Nov 2022
Modifié(e) : Conner Carriere le 11 Nov 2022
Thank you for this, but what would I do if for some reason there is a 0 in the grp matrix?
This is beacuse I have an instance of 1, 2, 3, no 4's, 5, 6, no 7, 8 9
Conner Carriere
Conner Carriere le 11 Nov 2022
That simple loop worked well for me! thank you!
Jan
Jan le 12 Nov 2022
"This is beacuse I have an instance of 1, 2, 3, no 4's, 5, 6, no 7, 8 9" - It is easier to write an answer, if you post the input data in a form, which can be used by copy&paste.
I'm glad, if the loop method solves the problem.
Conner Carriere
Conner Carriere le 21 Nov 2022
What would with that simple loop if I wanted to add another set of numbers to it. This one would be tied to the bounding box. Wherever the bounding box moves, the value would be "tied" to it and relocate to where it does. I changed the question to match
Conner Carriere
Conner Carriere le 21 Nov 2022
realized I can run the function twice and combine them... Its late

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by