Borrar filtros
Borrar filtros

"Index in position 1 exceeds array bounds" for deleting rows if containing string

1 visualización (últimos 30 días)
Hi everyone,
I am attempting to delete rows from the 500x5 table simResultsDomestic if they do not contain the string "Domestic", as below:
typeSample startSample durationSample endSample energy
__________ ___________ ______________ _________ _______
"Domestic" 999 690 249 11.413
"Domestic" 1242 970 772 16.044
"Domestic" 664 660 1324 10.916
"Fast" 932 1050 542 29.615
"Domestic" 967 590 117 9.7586
I have attempted two methods for this, but they both give me the same error message: "Index in position 1 exceeds array bounds (must not exceed 400)." 400 because for some reason my code manages to remove the correct rows (so the table reduces in size from 500 to 400 rows), yet then gives me the error message and stops running.
Method 1:
for z=1:height(simResultsDomestic)
if ~strcmp(simResultsDomestic.typeSample(z), "Domestic")
simResultsDomestic(z,:)=[];
end
end
Method 2:
for z=1:height(simResultsDomestic)
if simResultsDomestic.typeSample(z,:) ~= "Domestic"
simResultsDomestic(z,:)=[];
end
end
I feel like this might be an obvious one but I'm not able to find the solution.
Many thanks for any help,
Arnold

Respuesta aceptada

Joel Van Sickel
Joel Van Sickel el 19 de Ag. de 2020
Editada: Joel Van Sickel el 19 de Ag. de 2020
Hello Arnold,
try this with no loop:
toDelete = (simResultsDomestic.type == "Domestic");
simResultsDomestic(toDelete,:) = [];
Regards,
Joel

Más respuestas (0)

Categorías

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

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by