Effacer les filtres
Effacer les filtres

How to add values from array A (n,3) to array B(m,2)based on values equality between m and n?

1 vue (au cours des 30 derniers jours)
I have two arrays A and B of different sizes, wherein, array B is a subarray from array A. Therefore, I want to pad the array B with values from the third colums of array A(:,3) based on the equality comparison of B(ii,1) and A(ii,1).Resulted array should be B(m,3)
I have attached examples of dataset for the 2 arrays A and B namely pathInfo,and pathReduced respectively.

Réponse acceptée

Dyuman Joshi
Dyuman Joshi le 21 Juil 2023
You can directly get the desired result -
load('pathReduced.mat')
load('pathInfo.mat')
pathReduced
pathReduced = 6×2
40.0000 45.0000 35.6227 38.6501 31.9899 37.8378 16.2736 16.2026 17.8928 14.0324 17.0000 11.0000
inpath
inpath = 67×3
40.0000 45.0000 0 39.8859 44.7226 -0.0042 39.6225 44.5822 -0.0998 39.4387 44.3451 -0.1098 39.4530 44.0454 -0.1130 39.2219 43.8542 -0.1355 39.0537 43.6062 -0.1783 39.1261 43.3151 -0.1594 39.0572 43.0231 -0.1709 38.7575 43.0192 -0.2088
%Check which elements in the 1st column of inpath are common to the
%elements in the 1st column of pathReduced
idx = ismember(inpath(:,1),pathReduced(:,1));
%Get the sub array from inpath (as pathReduced is sub array of inpath as
%mentioned in the problem above)
out = inpath(idx,:)
out = 6×3
40.0000 45.0000 0 35.6227 38.6501 -0.3376 31.9899 37.8378 -0.1101 16.2736 16.2026 0.0521 17.8928 14.0324 -0.0284 17.0000 11.0000 0

Plus de réponses (0)

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by