how do I delete values from a matrix when my loop condition is false?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
I encountered a problem - my intention was to delete values in the matrix in certain conditions - the problem is that when I define my loop to delete item from my matrix in certain conditions when the loop keep going I get this message: >> Index of element to remove exceeds matrix dimensions. does someone has an idea to resolve my problem?
this is the important part of my code:
%
unitVec = round(0+(5).*rand(1,50));
bagUpVec = bagVec;
for jj = 1:size(bagVec,2);
if unitVec(jj)== 5;
bagUpVec(jj) == bagVec(jj)+10;
end
end
%
psychPass = zeros(1,50);
unitPass = zeros(1,50);
unitWait = zeros(1,50);
numReject = 0;
numPass = 0;
for jj = 1:size(bagUpVec,2);
if (bagUpVec(jj) >= 90) && (psychVec(jj)>= 700)
psychPass(jj) = psychVec(jj);
unitPass(jj) = unitVec(jj);
numPass = numPass + 1;
unitWait(jj)=[];
elseif (bagUpVec(jj) < 85) ...
&& (psychVec(jj)< 600)
numReject = numReject + 1;
unitWait(jj) = [];
else
psychPass(jj) = [];
unitPass(jj) = [];
unitWait (jj) = unitVec(jj);
end
end
0 Kommentare
Antworten (1)
Jan
am 23 Nov. 2013
Some hints:
unitVec = round(5 .* rand(1, 50));
bagUpVec = bagVec; % ??? What is "bagVec"?
index = (unitVec == 5);
bagUpVec(index) = bagUpVec(index) + 10;
Ich you delete unitWait(k), the higher elements are moved to the front, such that the former last element moves one index to the front:
a = [1,2,3]
a(1) = [] % a = [2, 3] !!
a(3) = [] % error !
A better method:
index = [1,3];
a(index) = [];
0 Kommentare
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!