How to replace 1 column in table

조회 수: 25 (최근 30일)
Cside
Cside 2023년 8월 2일
댓글: Sachin 2023년 8월 3일
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
Dyuman Joshi
Dyuman Joshi 2023년 8월 2일
Please attach your data using the paperclip button.

댓글을 달려면 로그인하십시오.

답변 (1개)

Sachin
Sachin 2023년 8월 2일
Hi Cside,
I understand that you want to replace 1 column in table.
Following steps might be helpful to you :
  1. First find the unique values in Table B using "unique".
  2. Find the count of those unique values using function "length".
  3. Create a vector representation from 1 to count.
  4. 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
unique_Age = 3×1
38 40 43
count_uniqueAge = length(unique_Age) % find the count of unique values
count_uniqueAge = 3
unique_ids = [1:1:count_uniqueAge] % create a vector
unique_ids = 1×3
1 2 3
dummyTableA
dummyTableA = 3×2
2 3 3 4 4 5
dummyTableA(:,1) = unique_ids; % update the value of column 1 in table A
dummyTableA
dummyTableA = 3×2
1 3 2 4 3 5
You can refer the following MATLAB Documentations for more information about above functions :
Thanks
Sachin
  댓글 수: 2
Cside
Cside 2023년 8월 2일
Hi Sachin,
Thank you for your reply. I would like the first column of dummyTableA to be 38;40;43 instead, not the number of unique IDs
Sachin
Sachin 2023년 8월 3일
Hi Cside,
Then you can simply do :
dummyTableA(:,1) = unique_Age;

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Preprocessing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by