How to fix error index exceeds matrix dimensions. I am very new to matlab and am trying to write a loop for this data set but I keep getting this error. Any help would be appreciated!
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Joshua Randle
am 2 Aug. 2018
Kommentiert: Stephen23
am 2 Aug. 2018
for i=1:length(Data);
for j=1:3
if Data(i,j)<=0
Data(i,:)=[];
end
end
end
1 Kommentar
Stephen23
am 2 Aug. 2018
This is MATLAB, so get rid of the ugly loop entirely and write simpler code:
>> Data = randi(9,6,3)-2
Data =
6 6 1
0 3 5
0 6 2
-1 4 -1
7 4 7
0 4 6
>> Data(any(Data<=0,2),:) = []
Data =
6 6 1
7 4 7
Akzeptierte Antwort
Dennis
am 2 Aug. 2018
You remove rows of Data, thus its size is shrinking during your loop. One way to resolve this is removing those rows after your loop:
idx=[];
for i=1:size(Data,1)
if any(Data(i,:)<=0)
idx=[idx,i];
end
end
Data(idx,:)=[];
2 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!