MATLAB: How to extract specific rows from one array to another array

arraycopyextractfilterintersect

Hi. I have these two arrays:
NA2 =
0 0.9800 1.0000 0.0200 -3.1000 1.0000 1.0000
0.1000 0.9800 0.0591 0.0200 -3.0000 1.0000 0.0779
0.2000 0.9800 0.0591 0.0200 -2.9000 1.0000 0.0779
0.3000 0.9800 0.0591 0.0200 -2.8000 1.0000 0.0779
0.4000 0.9800 0.0591 0.0200 -2.7000 1.0000 0.0779
0.5000 0.9800 0.0591 0.0200 -2.6000 1.0000 0.0779
0.6000 0.9800 0.0591 0.0200 -2.5000 1.0000 0.0779
0.7000 0.9800 0.0591 0.0200 -2.4000 1.0000 0.0779
0.8000 0.9800 0.0591 0.0200 -2.3000 1.0000 0.0779
0.9000 0.9800 0.0591 0.0200 -2.2000 1.0000 0.0779
1.0000 0.9800 0.0591 0.0200 -2.1000 1.0000 0.0779
1.1000 0.9800 0.0591 0.0200 -2.0000 1.0000 0.0779
1.2000 0.9800 0.0300 0.0200 -1.9000 1.0000 0.0494
1.3000 0.9800 0.0300 0.0200 -1.8000 1.0000 0.0494
1.4000 0.9800 0.0300 0.0200 -1.7000 1.0000 0.0494
1.5000 0.9800 0.0300 0.0200 -1.6000 1.0000 0.0494
1.6000 0.9800 0.0300 0.0200 -1.5000 1.0000 0.0494
1.7000 0.9800 0.0300 0.0200 -1.4000 1.0000 0.0494
1.8000 0.9800 0.0300 0.0200 -1.3000 1.0000 0.0494
1.9000 0.9800 0.0300 0.0200 -1.2000 1.0000 0.0494
2.0000 0.9800 0.0300 0.0200 -1.1000 1.0000 0.0494
2.1000 0.9800 0.0009 0.0200 -1.0000 1.0000 0.0209
2.2000 0.9800 0 0.0200 -0.9000 1.0000 0.0200
2.3000 0.9800 0 0.0200 -0.8000 1.0000 0.0200
2.4000 0.9800 0 0.0200 -0.7000 1.0000 0.0200
2.5000 0.9800 0.0009 0.0200 -0.6000 1.0000 0.0209
2.6000 0.9800 0.0009 0.0200 -0.5000 1.0000 0.0209
2.7000 0.9800 0.0009 0.0200 -0.4000 1.0000 0.0209
2.8000 0.9800 0 0.0200 -0.3000 1.0000 0.0200
2.9000 0.9800 0 0.0200 -0.2000 1.0000 0.0200
3.0000 0.9800 0.0009 0.0200 -0.1000 1.0000 0.0209
3.1000 0.9800 0 0.0200 -0.0000 1.0000 0.0200
3.2000 0.9800 0 0.0200 0.1000 0.0591 0.0012
3.3000 0.9800 0 0.0200 0.2000 0 0
3.4000 0.9800 0 0.0200 0.3000 0 0
3.5000 0.9800 0 0.0200 0.4000 0.0591 0.0012
3.6000 0.9800 0 0.0200 0.5000 0 0
3.7000 0.9800 0 0.0200 0.6000 0.0591 0.0012
3.8000 0.9800 0 0.0200 0.7000 0 0
3.9000 0.9800 0 0.0200 0.8000 0 0
4.0000 0.9800 0 0.0200 0.9000 0.0591 0.0012
4.1000 0.9800 0 0.0200 1.0000 0 0
4.2000 0.9800 0 0.0200 1.1000 0.0591 0.0012
4.3000 0.9800 0 0.0200 1.2000 0 0
4.4000 0.9800 0 0.0200 1.3000 0 0
4.5000 0.9800 0 0.0200 1.4000 0 0
4.6000 0.9800 0 0.0200 1.5000 0 0
4.7000 0.9800 0 0.0200 1.6000 0.0300 0.0006
4.8000 0.9800 0 0.0200 1.7000 0 0
4.9000 0.9800 0 0.0200 1.8000 0 0
5.0000 0.9800 0 0.0200 1.9000 0 0
5.1000 0.9800 0 0.0200 2.0000 0 0
5.2000 0.9800 0 0.0200 2.1000 0.0009 0.0000
5.3000 0.9800 0 0.0200 2.2000 0 0
5.4000 0.9800 0 0.0200 2.3000 0 0
5.5000 0.9800 0 0.0200 2.4000 0 0
5.6000 0.9800 0 0.0200 2.5000 0 0
5.7000 0.9800 0 0.0200 2.6000 0.0009 0.0000
5.8000 0.9800 0 0.0200 2.7000 0 0
5.9000 0.9800 0 0.0200 2.8000 0 0
6.0000 0.9800 0 0.0200 2.9000 0 0
6.1000 0.9800 0 0.0200 3.0000 0 0
6.2000 0.9800 0 0.0200 3.1000 0 0
CapacityOut =
0
1.1000
2.0000
3.1000
4.2000
5.1000
6.2000
Now, I am trying to "filter" NA2 using the values in CapacityOut. Meaning, I would like to extract rows which has the value of CapacityOut in the first column then output the new array to NA3.
I have this code which does the extraction:
z=0;
for i=1:size(NA2,1)
for j=1:size(CapacityOut,1)
if NA2(i,1)==CapacityOut(j,1)
z=z+1;
NA3(z,:)=NA2(i,:);
else
end
end
end
However, this is the result:
NA3 =
0 0.9800 1.0000 0.0200 -3.1000 1.0000 1.0000
1.1000 0.9800 0.0591 0.0200 -2.0000 1.0000 0.0779
2.0000 0.9800 0.0300 0.0200 -1.1000 1.0000 0.0494
4.2000 0.9800 0 0.0200 1.1000 0.0591 0.0012
6.2000 0.9800 0 0.0200 3.1000 0 0
It did not copy the rows with 3.1000 and 5.1000 on the first column. I have tried it on another set of array but with whole numbers on the first column and it works perfectly. Do you have any idea why?

Best Answer

The problem is that you test for equivalence of floating point values. This is bound to cause errors, as has been discussed hundreds of times on this forum:
etc., etc.
The solution is to use ismembertol, or compare the absolute difference against some tolerance value:
idx = false(size(NA2,1),1);
tol = 0.0001;
for k = 1:size(CapacityOut,1)
idx = idx | abs(NA2(:,1)-CapacityOut(k))<tol;
end
out = NA2(idx,:);
giving:
>> out
out =
0.00000 0.98000 1.00000 0.02000 -3.10000 1.00000 1.00000
1.10000 0.98000 0.05910 0.02000 -2.00000 1.00000 0.07790
2.00000 0.98000 0.03000 0.02000 -1.10000 1.00000 0.04940
3.10000 0.98000 0.00000 0.02000 -0.00000 1.00000 0.02000
4.20000 0.98000 0.00000 0.02000 1.10000 0.05910 0.00120
5.10000 0.98000 0.00000 0.02000 2.00000 0.00000 0.00000
6.20000 0.98000 0.00000 0.02000 3.10000 0.00000 0.00000
>>