I'm guessing you want something more complicated than
myArray = [1 2 3 4 5];
myArray2 = myArray(myArray > 3);
The easiest (but slowest) way to do what you're asking is something like
myArray2 = [];
for x = myArray
if CheckCondition(x) == 1
myArray2 = [myArray2 x]; %# grows myArray2, which is slow
end;
end;
You can sort of optimize this with something like
myArray2 = NaN(size(myArray));
ctr = 0;
for x = myArray
if CheckCondition(x) == 1
ctr = ctr + 1;
myArray2(ctr) = xx;
end;
end;
myArray2 = myArray2(1:ctr); %# drop the NaNs
You might also want to look into ARRAYFUN.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…