# Find value based on adjacent value condition and insert into new variable

3 views (last 30 days)
Fernando on 8 Sep 2022
Commented: Fernando on 8 Sep 2022
I have a variable that contains four columns. The first column has increment integers from 0 to 50. The fourth column has some random numbers.
Every number in the fourth column is associated with a number from the first column.
For example, 6.9119 is associated with 0 and -10.6901 to 1.
Now I have another variable where the first column has some integers.
These numbers are also between 0 and 50, but numbers might be repetitive in some rows.
I am looking to fill the second column of this new array with the corresponding value from the fourth column of the first array.
For example, everywhere that there is 0 in the first column, the value 6.9119 should be inserted into the second column.
Your help would be welcome.

Akira Agata on 8 Sep 2022
ismember function will be helpful for this task, like:
% Sample matrix
A = [(0:50)', rand(51,1)];
% Second matrix with index only
B = repelem((0:50)', 2);
% Apply ismember
[~, pt] = ismember(B, A(:,1));
% Add corresponding values to B
B = [B, A(pt, 2)];
% Show the result
disp(B)
0 0.4116 0 0.4116 1.0000 0.9358 1.0000 0.9358 2.0000 0.2611 2.0000 0.2611 3.0000 0.9179 3.0000 0.9179 4.0000 0.1945 4.0000 0.1945 5.0000 0.8413 5.0000 0.8413 6.0000 0.1172 6.0000 0.1172 7.0000 0.8611 7.0000 0.8611 8.0000 0.9853 8.0000 0.9853 9.0000 0.9129 9.0000 0.9129 10.0000 0.5301 10.0000 0.5301 11.0000 0.1679 11.0000 0.1679 12.0000 0.4269 12.0000 0.4269 13.0000 0.3499 13.0000 0.3499 14.0000 0.8446 14.0000 0.8446 15.0000 0.2920 15.0000 0.2920 16.0000 0.6363 16.0000 0.6363 17.0000 0.1607 17.0000 0.1607 18.0000 0.6755 18.0000 0.6755 19.0000 0.5971 19.0000 0.5971 20.0000 0.6314 20.0000 0.6314 21.0000 0.8578 21.0000 0.8578 22.0000 0.4748 22.0000 0.4748 23.0000 0.8513 23.0000 0.8513 24.0000 0.0575 24.0000 0.0575 25.0000 0.1833 25.0000 0.1833 26.0000 0.4709 26.0000 0.4709 27.0000 0.8045 27.0000 0.8045 28.0000 0.4485 28.0000 0.4485 29.0000 0.6027 29.0000 0.6027 30.0000 0.1016 30.0000 0.1016 31.0000 0.5743 31.0000 0.5743 32.0000 0.5373 32.0000 0.5373 33.0000 0.4140 33.0000 0.4140 34.0000 0.2825 34.0000 0.2825 35.0000 0.7808 35.0000 0.7808 36.0000 0.2294 36.0000 0.2294 37.0000 0.9534 37.0000 0.9534 38.0000 0.3508 38.0000 0.3508 39.0000 0.0983 39.0000 0.0983 40.0000 0.7216 40.0000 0.7216 41.0000 0.8293 41.0000 0.8293 42.0000 0.5657 42.0000 0.5657 43.0000 0.0617 43.0000 0.0617 44.0000 0.2747 44.0000 0.2747 45.0000 0.3750 45.0000 0.3750 46.0000 0.0057 46.0000 0.0057 47.0000 0.3309 47.0000 0.3309 48.0000 0.3858 48.0000 0.3858 49.0000 0.6357 49.0000 0.6357 50.0000 0.1620 50.0000 0.1620
##### 1 CommentShow NoneHide None
Fernando on 8 Sep 2022
Thank you for your quick and detailed response.

### Categories

Find more on Operators and Elementary Operations 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!