Find value based on adjacent value condition and insert into new variable
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Fernando
el 8 de Sept. de 2022
Comentada: Fernando
el 8 de Sept. de 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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1119910/image.png)
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1119915/image.png)
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.
0 comentarios
Respuesta aceptada
Akira Agata
el 8 de Sept. de 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)
Más respuestas (0)
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!