How do I remove every row that has a zero in it?
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Rocco
am 30 Mär. 2023
Kommentiert: Rocco
am 30 Mär. 2023
I have a 366x7 matrix, and I'm trying to remove each row that has a zero in it, I would prefer to use for loops instead of some built-in function.
so for example
TSLA = rand(366,7)
for col = 1:1:size(TSLA,1)
if TSLA(col,4) == 0
TSLA(col,:) = []
end
end
for whatever reason it removes like 61 rows, but leaves like 20 or so with zeros.
Then it says "Index in position 1 exceeds array bounds. Index must not exceed 305." Even though the initial matrix is 366...
0 Kommentare
Akzeptierte Antwort
Weitere Antworten (1)
Image Analyst
am 30 Mär. 2023
Try this:
% Check which rows have a zero in one or more columns:
zeroRows = any(TSLA == 0, 2); % Logical index. Equals true if a zero in the row, false if no zero in the row.
% Delete those rows:
TSLA(zeroRows, :) = []; % Delete the row by setting all column values of the row to null.
3 Kommentare
Siehe auch
Kategorien
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!