How to replace 1 column in table
25 次查看(过去 30 天)
显示 更早的评论
Hello!
I have a NaN table (100x200), lets call it A, and a large dataset with first column being participants ID (eg 123AJH), lets call this B. I want to find the unique participant ID in B (:,1) (eg there are 100 unique IDs as many of them are repeated) and replace the unique IDs as the first column of A. However, the result is 1,2,...100 (the number of unique ID) rather than the ID itself (123AJH).
Anyone knows how to replace 1st column of A with the column of unique IDs found in B (123AJH; 232AJH, ...) ?
Much appreciated thank you. The code I wrote is below:
A (:,1) = unique (B(:,1));
回答(1 个)
Sachin
2023-8-2
Hi Cside,
I understand that you want to replace 1 column in table.
Following steps might be helpful to you :
- First find the unique values in Table B using "unique".
- Find the count of those unique values using function "length".
- Create a vector representation from 1 to count.
- Then assign the values to the Table A.
dummyTableA = [2,3;3,4;4,5]; %dummy table a
Age = [38;43;38;40;38];
unique_Age = unique(Age) % find the unique values
count_uniqueAge = length(unique_Age) % find the count of unique values
unique_ids = [1:1:count_uniqueAge] % create a vector
dummyTableA
dummyTableA(:,1) = unique_ids; % update the value of column 1 in table A
dummyTableA
You can refer the following MATLAB Documentations for more information about above functions :
Thanks
Sachin
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Preprocessing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!