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

1 view (last 30 days)
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

Accepted Answer

Ted Shultz
Ted Shultz on 21 Aug 2019
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

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!