我有一些数据,我想从中获取一些信息。但是,我遇到了一些问题,我很乐意获得专家的帮助。获取矩阵和列匹配
数据和一些信息:
A = [1 0 -1 2 0 1;0 2 1 0 1 -2;1 1 0 2 1 1]%matrix
B = [1 3]#rows 1 and 3 are rows for searching.
struc.names = ['blue', 'red', 'green', 'amber', 'grey','yellow']% a structure of column names.
required.names = {'blue', 'green', 'grey','yellow'}; % a structure of required column names
我试图获得3种类型的信息如下:
第一:获取并保存矩阵行的子集。
第二:我想获得一个载体(填充有1或0),对应于比较struc.names
第三感兴趣(required.names)列:对于行1和3,发现之间的匹配行元素不为零时的struc.names和required_rows;还要根据匹配的数量排列结果输出。
问题1:获得
code for getting matrix:
struc.names = {'blue', 'red', 'green', 'amber', 'grey','yellow'};
required_rows = [1 3];
for k = 1:length(required_rows);
% open file for writing
fid =fopen('ouput.txt','w');
idx(k,:) = A(required_rows(k),:);
fprintf(fid,'%d \n',idx);#print matrix
end;
输出:
1 0 -1 2 0 1 1 1 0 2 1 1
所需的输出:
1 0 -1 2 0 1
1 1 0 2 1 1
问题2:获取required.names = { '蓝' 的列向量, 'green','gray','yellow'}与struc.names相比;
我想在像1 [1 0 1 0 0 1]这样的向量中获得1(列名存在)和0(列名不存在);我不确定如何编写代码。
问题3:在行元素非零时查找struc.names和required_rows之间的匹配,然后根据匹配数获得排序结果的代码。代码:
要求struc.names = ['blue', 'red', 'green', 'amber', 'grey','yellow']# a structure of column names.
required.names = {'blue', 'green', 'grey','yellow'}; # a structure of required column names
struc.names = {'blue', 'red', 'green', 'amber', 'grey','yellow'}
required_rows = [1 3];
% open file for writing, and Loop
fid=fopen('file.txt','w+');
for K = 1 : length(required_rows);
idx = A(required_rows(K),:) ~= 0;
if any(idx)
struc.names = struc.names(idx)
C = intersect(struc.names ,required_rows)
fprintf(fid, 'row A(%d,:) has the following matches:\n');
fprintf(fid, '%s ', C{idx});
fprintf(fid, '\n');
end
end
fclose(fid);
有序输出(根据比赛的号码):
row 3: blue red amber grey yellow
row 1: blue green amber yellow
谢谢
也许,你应该把它分成三个问题...... – Oli