MATLAB Answers

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)
Seba.V
Seba.V on 21 Aug 2019
Answered: Ted Shultz on 21 Aug 2019
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

  0 Comments

Sign in to comment.

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

  0 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by