How can I generate an array that keeps all the values related to specific indices keeps the zero as reference value?

2 ビュー (過去 30 日間)
Given the array x=[ -2 -1 0 1 2 ] with respective array y=[2 1 3 5 4] and a value n=2
How can I generate a code that divides all the numbers of x by n, and generates a new array with only the values resulting from the division that are integers?
How can i then generate a new y array where only the values that were related to the (x/n = integers values) are included?
ie: if x=[ -2 -1 0 1 2 ], y=[2 1 3 5 4] and n=2
new_x=[ -1 0 1 ], new_y= [ 2 3 4 ]
or if
x=[ -3 -2 -1 0 1 2 3 ], y=[7 2 1 3 5 4 6] and n=3
new_x=[ -1 0 1 ], new_y= [ 7 3 6]
I hope it makes sense!
Thank you in advance

採用された回答

Ted Shultz
Ted Shultz 2019 年 8 月 21 日
The following code does what you want for the values you gave as examples
hope this helps,
--ted
y=[2 1 3 5 4]
x=[ -2 -1 0 1 2 ]
n=2
tempX=x/n; % find x/n
keepIndex = tempX==floor(tempX); % find locations where these are integers
newX= tempX(keepIndex) % only keep values at those locations
newY=y(keepIndex)% only keep values at those locations

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by