Unable to use a value of type cell as an index

7 visualizaciones (últimos 30 días)
Jiongyi Meng
Jiongyi Meng el 10 de Mayo de 2022
Comentada: Jiongyi Meng el 11 de Mayo de 2022
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 comentarios
Fangjun Jiang
Fangjun Jiang el 10 de Mayo de 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 el 10 de Mayo de 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.

Iniciar sesión para comentar.

Respuesta aceptada

Voss
Voss el 11 de Mayo de 2022
Editada: Voss el 11 de Mayo de 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 comentarios
Jiongyi Meng
Jiongyi Meng el 11 de Mayo de 2022
Thank you so much, appreciate!
Jiongyi Meng
Jiongyi Meng el 11 de Mayo de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by