フィルターのクリア

adding zeros to empty spaces of an array

6 ビュー (過去 30 日間)
Samuel Oyeleye
Samuel Oyeleye 2021 年 7 月 26 日
回答済み: Sruthi Gundeti 2021 年 7 月 26 日
Assuming you have the following (note: the numbers are irrelevant...it's the process I need to understand)
A = [ 1 4 7 8 12]
B = [3 5 10]
and you want to match the two as 1 even though they are uneven as follows, but you want to match the remaining values with zeros (as follows:)
[3,1] (1 from A and 3 from B)
[7,5] (7 from A and 5 from B)
[12,10] (12 from A and 10 from B)
while the remaining unmatched numbers in A you want as follows
[4, 0] (4 in A matched with 0)
[8,0] (8 in A matched with 0)
Note: the 0's will be added to the B to make it the same size as A
PLEASE HOW WOULD YOU GO ABOUT THIS.....APPRECIATE ANY HELP

回答 (1 件)

Sruthi Gundeti
Sruthi Gundeti 2021 年 7 月 26 日
I am able to get output C =[ 3 0 5 0 10] with the following code . I used setdiff function which is same as difference function in sets
A = [ 1 4 7 8 12]
B = [3 5 10]
C=B(1)
for i=2:length(A)
if mod(i,2)==0
C(i)=0
end
C=[C setdiff(B,C)]
end

カテゴリ

Help Center および File ExchangeData Types についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by