How can I remove double values in an array
3 次查看(过去 30 天)
显示 更早的评论
i have these set of two arrays A and B,
A=
[4, 3.40 ;
6, 3.20;
7, 5.50 ;
9, 6.13;
11, 7.18;
14, 8.23;
15, 9.28;
18, 10.33;
20, 11.38 ]
B = [
1, 0 ;
2, 0;
3, 0;
4, 0 ;
5, 0 ;
6, 0 ;
7, 0;
8, 0;
9, 0 ;
10, 0 ;
11, 0;
12, 0;
13, 0;
14, 0;
15, 0;
16, 0;
17, 0;
18, 0;
19, 0 ;
20, 0 ]
and I concatenate them to get C:
C = [
1, 0;
2, 0;
3, 0;
4, 3.40;
4, 0;
5, 0;
6, 3.20;
6, 0;
7, 5.50;
7, 0;
8, 0;
9, 6.13;
9, 0;
10, 0;
11, 7.18;
11, 0;
12, 0;
13, 0 ;
14, 8.23;
14, 0 ;
15, 9.28;
15, 0;
16, 0;
17, 0;
18, 10.33;
18, 0;
19, 0;
20, 11.38 ;
20, 0 ]
The first array contains subsets of the second array. Please how can I remove the rows in C that has zero in the second column.
Thanks
0 个评论
采纳的回答
Voss
2023-1-4
It's better to build C the way you want it from the start:
A=[4, 3.40 ;
6, 3.20;
7, 5.50 ;
9, 6.13;
11, 7.18;
14, 8.23;
15, 9.28;
18, 10.33;
20, 11.38 ];
B = [
1, 0 ;
2, 0;
3, 0;
4, 0 ;
5, 0 ;
6, 0 ;
7, 0;
8, 0;
9, 0 ;
10, 0 ;
11, 0;
12, 0;
13, 0;
14, 0;
15, 0;
16, 0;
17, 0;
18, 0;
19, 0 ;
20, 0 ];
C = B;
[ism,idx] = ismember(A(:,1),B(:,1));
C(idx(ism),2) = A(ism,2);
disp(C);
更多回答(1 个)
MarKf
2023-1-4
"remove rows in C that has zero in the second column" so pretty much the array B but with the 2nd column values of A whenever their 1st column values match? if that's the case:
A = [4, 3.40 ; 6, 3.20; 7, 5.50 ; 9, 6.13; ];
B = [(1:10)', zeros([10 1])];
C = B;
C(ismember(B(:,1),A(:,1)),2)=A(:,2)
Though I guess that works only if the values in A's 1st column are all contained in B's 1st column. Otherwise you can specify:
C(ismember(B(:,1),A(:,1)),2)=A(ismember(A(:,1),B(:,1)),2);
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!