how do I delete values from a matrix when my loop condition is false?
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Jan
2013-11-23
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!