match vector with first column of matrix
1 次查看(过去 30 天)
显示 更早的评论
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 个)
David Hill
2020-2-5
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!