Thursday, January 5, 2012

finding and indexing in matlab

This is something as ridiculous as in 2001 when I didn't know how to use .ppt so I just made .html presentations.

Well I don't know procedural SQL so I'm using matlab to find and index things.

The problem:
I had a big table in a database and you want to fill in a certain column with an index that depends on other relationships in that table.  (Basically, filling in parent-child relationships but each row has several identifiers: name, unique ID, other unique ID.)

The first loop demonstrates how to use "find" with strings: find the index in the array "name" where the cell value (string) equals input_name(i).

for i = 1:length(input_name)
    igsn_output(i) = igsn(find(ismember(name, input_name(i))==1))
end
igsn_output = transpose(igsn_output);

This second one is just weird, it was giving string/cell/ etc. errors but this worked (input_parent was a number but in a cell array).

for i = 1:length(input_name)
    parent_id_output{i} = cell2mat(sample_id(find(ismember(igsn, input_parent(i))==1)))
end
parent_id_output = transpose(parent_id_output);

I pasted it all into excel and then used "concatenate" to form the SQL statements.

This all could've been done in the SQL environment if I knew procedural postgreSQL.

They shouldn't let scientists code.