I have two arrays:
A = [0, 1]; and B = [0, 1, 3, 1, 4, 5, 6];
I want to compare the first element of A to the first 3 elements of B and the second element of A to the next 4 elements of B. If the elements of A are equal I delete it from B. So in simple:
if (A(1) == B(1:3))
delete A(1) from B
Similarly
if (A(2) == B(4:7))
delete A(2) from B
Is there any way I can do this without having to enter the indices in a hard coded manner?
So in the end B should have the following elements:
B = [1, 3, 4, 5, 6];

3 件のコメント

Jan
Jan 2018 年 6 月 22 日
The question is not clear. What if A(1) is 1? Then 1 is found in the first block of B. Should B be set to
[0, 3, 1, 4, 5, 6];
afterwards, or to
[0, 3, 4, 5, 6];
Or should the elements of B removed at the end only after the matching elements have been found?
So what are the wanted outputs for:
A = [0, 1]
B1 = [0, 1, 2, 0, 1, 2]
B2 = [0, 0, 0, 1, 2, 3]
B3 = [1, 1, 2, 1, 1, 2]
?
CharlesB
CharlesB 2018 年 6 月 22 日
Sorry for not being clear the first array A will always have elements in the following sequential order 0, 1, 2 ..... The second Array B will always have an element of B so B can be [0,1,2, 0,1, 3, 1,2,5] and B has to become [1,2, 0,3, 1,5] as I removed the elements of B after comparing them as I explained above. The wanted outputs for B1 = [1,2, 0, 2]; For B2 = [2,3]; For B3 = [1,1,2, 2]. In this case I compared the elements of A for every 3 elements of B. In the above example I compared A(1) to the first 3 elements of B and A(2) to the next 4 elements of B. The comparison between 1 element of A to the elements of B can vary. What does not change is the sequential elements of A they will always b 0, 1, 2, 3,... Please stick to my question as it will get even more confusing.
Jan
Jan 2018 年 6 月 22 日

サインインしてコメントする。

 採用された回答

KSSV
KSSV 2018 年 6 月 22 日
編集済み: KSSV 2018 年 6 月 22 日

0 投票

A = [0, 1];
B = [0, 1, 3, 1, 4, 5, 6];
C{1} = B(1:3) ;
C{2} = B(4:end) ;
for i = 1:2
C{i}(C{i}==A(i))=[] ;
end
cell2mat(C)

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrices and Arrays についてさらに検索

質問済み:

2018 年 6 月 22 日

コメント済み:

Jan
2018 年 6 月 22 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by