how do I delete values from a matrix when my loop condition is false?

2 views (last 30 days)
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

Answers (1)

Jan
Jan on 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) = [];

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!