필터 지우기
필터 지우기

adding different number of columns to specific row

조회 수: 3 (최근 30일)
Shayma
Shayma 2020년 1월 30일
댓글: Shayma 2020년 2월 6일
Hi,
I have 2 differnt csv files, i want to append the columns from book8 to book 7 based on the name column that appears in the 2 files, the name in 7 are unique but in 8 there are duplicates, so the appending columns may differ from row to another ...
is there a way to add the corresponding columns to a specific row?
i tried to append the columns in differnt ways with no success...
[M_obs,text_obs,obs]=xlsread('book7.csv');
[M_pri,text_pri,pri]=xlsread('book8.csv');
[r,c]=size(obs);
[new_matrix]=deal({});
new_matrix=obs;
for row=(2:r) %
z=find(strcmp(pri(row,1),obs(:,1)))
new_matrix(row,:)=pri(z,3:end);
end

답변 (1개)

Ridwan Alam
Ridwan Alam 2020년 1월 30일
편집: Ridwan Alam 2020년 1월 30일
Try using the table join() method:
Example:
M_obs = readtable('book7.csv');
M_pri = readtable('book8.csv');
[~,M_pri_idx] = unique(M_pri(:,1));
M_pri_new = M_pri(M_pri_idx,:);
new_table = join(M_obs,M_pri_new,'Keys',[1,1]);
Hope this helps.
  댓글 수: 3
Ridwan Alam
Ridwan Alam 2020년 2월 2일
Sorry, but the question is a bit confusing.
You wrote: if book8.csv has two rows of the entry "zinc543", it will append these two rows as new columns for the entry "zinc543" in book7. Can you mention which row in book 7 and book 8 has this?
Are you trying to extend the table_book_8 with columns from table_book_7 wherever available? If yes, try this code:
A=readtable('book7.csv');
B=readtable('book8.xlsx');
zSize = [size(B,1), size(B,2) + size(A,2) - 1];
zType = ["string",repmat("doublenan",[1,zSize(2)-1])];
zNew = table('Size',zSize,'VariableTypes',zType);
zVar = {B.Properties.VariableNames{:},A.Properties.VariableNames{2:end}};
zNew.Properties.VariableNames = zVar;
zNew(:,1:width(B))=B;
for i = 1:height(zNew)
zAind = find(table2array(A(:,1))==table2array(zNew(i,1)));
if ~isempty(zAind)
zNew(i,1+width(B):end) = A(zAind,2:end);
end
end
% zNew contains all of B, and adds A to wherever available
Shayma
Shayma 2020년 2월 6일
sorry, I update the files, im trying to extend book7.csv with the columns from book8.csv
ZINC02619534 in row 3 at book7 will have the columns (2:end) from book8, from the rows 3,6,7
while ZINC02638599 for example will have the columns from book8 - row 13 only because it appears only once ..

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

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by