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

1 次查看(过去 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 CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by