Matching Values from one Table to Another
조회 수: 10 (최근 30일)
이전 댓글 표시
Hi everyone,
Is there a way from me to match the numbers in two dataset and extract the values in the row?
For instance,
This is my first table:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1337774/image.jpeg)
This is my second table:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1337779/image.jpeg)
What is would like to do---> I would like to search for my second table values in the first table, specifically under "FirstVID" and "SecondVID". Once I have a match of these values, I would like the numbers on "FirstVID" or "SecondVID" to be replaced by the string "AV".
Is this posssible?
Thank you.
댓글 수: 0
채택된 답변
Walter Roberson
2023년 3월 27일
Change
FirstVID = NewTab (:,["FirstVID"])
to
FirstVID = NewTab {:,["FirstVID"]};
추가 답변 (1개)
Joe Vinciguerra
2023년 3월 27일
Veh = [2007; 1265; 1266; 1269];
FirstVID = [1881; 1269; 2007];
SecondVID = [1892; 2188; 1266];
T = table(FirstVID, SecondVID);
% find data common to each table
[~, ~, ib1] = intersect(Veh, T.FirstVID);
[~, ~, ib2] = intersect(Veh, T.SecondVID);
% Convert to cell since you want mixed data types
T.FirstVID = num2cell(T.FirstVID);
T.SecondVID = num2cell(T.SecondVID);
% using the indexing found above, replace with "AV"
T.FirstVID(ib1) = {'AV'};
T.SecondVID(ib2) = {'AV'};
disp(T)
Alternately, you could avoid converting to cells and simply repalce the indexed values with NaN, unless having it read "AV" is necessary.
댓글 수: 2
Joe Vinciguerra
2023년 3월 28일
Glad you were able to resolve your error in the other thread. Please accept my answer.
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!