Obtain matrix and column matches

Hi,
I have some data from which I want to obtain some information. However, I ran into some problems for which I will be glad to get expert help.
Data and some information:
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
I tried to obtain 3 types of information as follows:
First: Get and save matrix for a subset of rows.
Second: I want to obtain a vector (populated with either 1 or 0) corresponding to columns of interest (required.names) compared to struc.names
Third: For rows 1 and 3, find matches between struc.names and required_rows when row elements are non-zero; also to have result output arranged according to the number of matches.
Problem 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;
output obtained:
1
0
-1
2
0
1
1
1
0
2
1
1
required output:
1 0 -1 2 0 1
1 1 0 2 1 1
Problem 2: Obtain column vector for required.names = {'blue', 'green', 'grey','yellow'} when compared with struc.names;
I want to obtain 1(column name present) and 0(column name absent) in a vector like: [1 0 1 0 0 1]; I'm unsure how to write the code.
Problem3: Code for finding matches between struc.names and required_rows when row elements are non-zero and then obtaining sorted results arranged according to number of matches.
code:
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);
Sorted output (according to number of matches) required as:
row 3: blue red amber grey yellow
row 1: blue green amber yellow
Thank you

Answers (0)

Categories

Find more on Computational Geometry in Help Center and File Exchange

Asked:

on 21 Sep 2012

Community Treasure Hunt

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

Start Hunting!