필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
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

채택된 답변

Joel Van Sickel
Joel Van Sickel 2020년 8월 19일
편집: Joel Van Sickel 2020년 8월 19일
Hello Arnold,
try this with no loop:
toDelete = (simResultsDomestic.type == "Domestic");
simResultsDomestic(toDelete,:) = [];
Regards,
Joel
  댓글 수: 1
Arnold Kuersteiner
Arnold Kuersteiner 2020년 8월 19일
Thank you, that worked! Just changed the == to ~= as I wanted to remove those without "Domestic".

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by