Unable to use a value of type cell as an index

15 views (last 30 days)
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
Fangjun Jiang
Fangjun Jiang on 10 May 2022
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.
dpb
dpb on 10 May 2022
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

Voss
Voss on 11 May 2022
Edited: Voss on 11 May 2022
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
Jiongyi Meng
Jiongyi Meng on 11 May 2022
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

Find more on Startup and Shutdown in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!