Unable to use a value of type cell as an index

I am working on a cell merge, the following is my idea:
1. Set the logical value of all rows to 1.
2. Start from the first row with a logical value of 1, and traverse all subsequent rows with a logical value of 1.
3. If the first cell of the traversed row is exactly the same as the first cell of this row, merge all the columns of the two rows.
4. After the merge is complete, set the logical value of the merged row to 0.
5. Continue to traverse the lines after merging until the end of the traversal.
6. Finally, jump to the second step to continue to find the next row with a logical value of 1 and traverse all the rows with a logical value of 1 after this row and merge them.
7. When all traversals are over, extract all rows with a logical value of 1 and combine them into a new mxn cell.
The following is my code, but the following problem occurs when setting the logical value to the row of the cell: Unable to use a value of type cell as an index.
Please tell me how to modify the code to achieve the effect of encoding the logical value of the row of the cell, thanks
f = true(1, length(bbox_cell));
for i=1:length(bbox_cell)
if f(bbox_cell(i))
a = bbox_cell{i,1};
for j=i+1:length(bbox_cell)
if f(bbox_cell(j))
strcmp(a, bbox_cell{j,1});
if 1
bbox_cell{i,1} = [bbox_cell{i,1}];
bbox_cell{i,2} = [bbox_cell{i,2}; bbox_cell{j,2}];
bbox_cell{i,3} = [bbox_cell{i,3}; bbox_cell{j,3}];
f(bbox_cell(j)) = false;
end
end
end
end
end
bbox_cell = bbox_cell(f)
And here is my cell for reference:

5 Comments

Hi,
Not sure to understand what you are trying to do.
Could you show an exemple of what you would like to have after you run your code?
GL.
Well, w/o the full error message, we don't even know which line of code is the offending one...
You could optimize the above logic quite a lot by using logical indexing -- you can find all matching cell content in one call.
You could also help a bunch in the search if you were to sortrows() on the basis of the first cell; then all rows until there's a difference between two cells are a match without any searching. Turning that cell into a categorical variable would aid in that distinction.
Hi, let me rephrase my purpose. I want to combine the data of each row with the same first column, and combine all the columns of the row with the same file name into one cell, so as to achieve the effect of marking multiple labels for one data sample. And the problem is in the following line of code:
if f(bbox_cell(i))
it prompts me the error: Unable to use a value of type cell as an index.
Now I want to know how to correctly encode each row with the correct logical value, could you please help me, thanks.
Hard to understand your question, but it should be easy to debug this problem.
Assign i=1, show the value of bbox_cell(i), then show the value of f(bbox_cell(i))
f is a logical array, f(1) and f(2) etc. are all true. But your data show that bbox_cell(i) is nothing like 1, 2, 3,etc. You can't use bbox_cell(i) to index a value in the array of f.
That is what the error message says. You are trying to use a value of type cell as an index, which you can't do.
Well, yes, that's the problem statement you gave before -- all the comments on "how" I made above are still valid and would simplify the code significantly if you were to use them.
As for the latter, you need
if f(bbox_cell{i})
probably, but as the other poster says, the code is convoluted enough I didn't really even try to read it...if you have this cell dereferencing issue there, you'll probably find you've got more of the same elsewhere.

Sign in to comment.

 Accepted Answer

Maybe the function compact_bbox defined below is close to what you have in mind.
bbox_cell = { ...
'img1' [1 2 3] 'pothole'; ...
'img1' [4 5 6] 'pothole'; ...
'img2' [1 2 3] 'pothole'; ...
'img2' [4 5 6] 'pothole'; ...
'img1' [7 8 9] 'pothole'; ...
}
bbox_cell = 5×3 cell array
{'img1'} {[1 2 3]} {'pothole'} {'img1'} {[4 5 6]} {'pothole'} {'img2'} {[1 2 3]} {'pothole'} {'img2'} {[4 5 6]} {'pothole'} {'img1'} {[7 8 9]} {'pothole'}
bbox_cell_new = compact_bbox(bbox_cell)
bbox_cell_new = 2×3 cell array
{'img1'} {3×3 double} {3×7 char} {'img2'} {2×3 double} {2×7 char}
But how are you going to combine cells whose contents are incompatible sizes?
bbox_cell{2,3} = 'potholio';
bbox_cell_new = compact_bbox(bbox_cell)
Error using vertcat
Dimensions of arrays being concatenated are not consistent.

Error in solution>compact_bbox (line 19)
bbox_cell{i,3} = [bbox_cell{i,3}; bbox_cell{j,3}];
function bbox_cell = compact_bbox(bbox_cell)
N = size(bbox_cell,1);
f = true(N,1);
for i=1:N
a = bbox_cell{i,1};
for j=i+1:N
if f(j) && strcmp(a, bbox_cell{j,1});
bbox_cell{i,2} = [bbox_cell{i,2}; bbox_cell{j,2}];
bbox_cell{i,3} = [bbox_cell{i,3}; bbox_cell{j,3}];
f(j) = false;
end
end
end
bbox_cell = bbox_cell(f,:);
end

2 Comments

Thank you so much, appreciate!
There is no need to worry about compatibility issues, because the format of the columns of the table is exactly the same, and there is no size incompatibility.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2021b

Asked:

on 10 May 2022

Commented:

on 11 May 2022

Community Treasure Hunt

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

Start Hunting!