how do I delete values from a matrix when my loop condition is false?
1 回表示 (過去 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!