match vector with first column of matrix

1 vue (au cours des 30 derniers jours)
SChow
SChow le 5 Fév 2020
Modifié(e) : Stephen23 le 5 Fév 2020
I have a matrix 'Mat' and a vector 'v', I want to match the vector 'v' with the first row of 'Mat' such that I get an output 'res'. I tried using the ismember function and a code mentioned below
Mat=[1 356
4 457
7 91
6 431
9 33
10 322 ]
v =[NaN 1 7 NaN 1 9 6 NaN 6 6]
res= [NaN NaN
1 356
7 91
NaN NaN
1 356
9 33
6 431
NaN NaN
6 431
6 431]
%%%%%%code I tried but did not work, it does not give numbers for repetative occurences of elemet in vector B
idx=setdiff(v,Mat(:,1)).';
ans=zeros(numel(idx),1);
idx=[idx' ans];
res=sortrows([Mat;idx]);
  1 commentaire
SChow
SChow le 5 Fév 2020
I tried a different code, but as the original length of v is 37324800x1 double, Matlab could not process the data
v=v';
bsxfun(@minus,Mat(:,1),scode);
[~,minRow] = min(abs(bsxfun(@minus,Mat(:,1),v)));
res = [ v.', Mat(minRow,2:end) ];

Connectez-vous pour commenter.

Réponse acceptée

Stephen23
Stephen23 le 5 Fév 2020
Modifié(e) : Stephen23 le 5 Fév 2020
Simpler with just one ismember call and some basic indexing:
>> [idx,idy] = ismember(v(:),Mat(:,1));
>> res = [v(:),v(:)];
>> res(idx,2) = Mat(idy(idx),2)
res =
NaN NaN
1 356
7 91
NaN NaN
1 356
9 33
6 431
NaN NaN
6 431
6 431

Plus de réponses (1)

David Hill
David Hill le 5 Fév 2020
res=[v',nan(length(v),1)];
a=unique(v);
a=a(~isnan(a));
for j=1:length(a)
res(ismember(v,a(j)),2)=Mat(ismember(Mat(:,1),a(j)),2);
end

Catégories

En savoir plus sur Logical 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