Prev: surfer matlab vector quiver
Next: dB Conversion
From: Pr B on 19 May 2010 18:00 i'm trying to create a matrix X of 0/1 values from the following information: i have data as a cell (a toy example) in the following format: data = {a b2; a c6; a a7; b b2; b d3; b c6}; a b2 a c6 a a7 b b2 b d3 b c6 .... i want to create a matrix X where X =zeros (length(unique(data(:,1))), length(unique(data(:,2)))). since we have 2 unique values in column 1 of data and 4 unique values in column 2 of data, X will be a 2x4 matrix. for the first row of X, corresponding to the entry 'a', i want a 1 wherever an element from column 2 is present. same for the entry 'b'. in the above example, X should look like: X = [1 1 1 0; 0 1 1 1] note the entries in the columns of X are sorted in alphabetical order due to unique(data(:,2)). how would i go about doing this? i'm trying to implement this using for loops, but i'm getting confused since i seemingly need 3 for loops over the unique columns of data and the total length of data. any help would be appreciated!
From: TideMan on 19 May 2010 18:17 On May 20, 10:00 am, "Pr B" <pb2...(a)columbia.edu> wrote: > i'm trying to create a matrix X of 0/1 values from the following information: > > i have data as a cell (a toy example) in the following format: > > data = {a b2; a c6; a a7; b b2; b d3; b c6}; > > a b2 > a c6 > a a7 > b b2 > b d3 > b c6 > ... > > i want to create a matrix X where X =zeros (length(unique(data(:,1))), length(unique(data(:,2)))). since we have 2 unique values in column 1 of data and 4 unique values in column 2 of data, X will be a 2x4 matrix. for the first row of X, corresponding to the entry 'a', i want a 1 wherever an element from column 2 is present. same for the entry 'b'. in the above example, X should look like: > > X = [1 1 1 0; 0 1 1 1] > > note the entries in the columns of X are sorted in alphabetical order due to unique(data(:,2)). > > how would i go about doing this? i'm trying to implement this using for loops, but i'm getting confused since i seemingly need 3 for loops over the unique columns of data and the total length of data. any help would be appreciated! C'mon, show us your code. Don't be shy.
From: Pr B on 19 May 2010 18:38 ok, here you go: importdata('data.txt'); unique_mods = unique(data(:,1)); unique_tgs = unique(data(:,2)); X = zeros(length(unique_mods),length(unique_tgs)); for i = length(unique_mods) for k = 1:length(data) mod = data(k,2) tg_index = find(unique_tgs = mod); X(i,tg_index) = 1; end end i think i need an if statement somewhere in there and i'm pretty sure this is broken in other places too, but i think i have the general framework down?
From: TideMan on 19 May 2010 19:02 On May 20, 10:38 am, "Pr B" <pb2...(a)columbia.edu> wrote: > ok, here you go: > > importdata('data.txt'); > unique_mods = unique(data(:,1)); > unique_tgs = unique(data(:,2)); > > X = zeros(length(unique_mods),length(unique_tgs)); > > for i = length(unique_mods) > for k = 1:length(data) > mod = data(k,2) > tg_index = find(unique_tgs = mod); > X(i,tg_index) = 1; > end > end > > i think i need an if statement somewhere in there and i'm pretty sure this is broken in other places too, but i think i have the general framework down? I don't know why you need two loops. Won't this do? for k = length(unique_mods) mod = data(k,2) tg_index = find(unique_tgs = mod); X(k,tg_index) = 1; end Note: it's a BAD idea to use i as an index because by default it is sqrt(-1). Same applies to j.
From: Pr B on 19 May 2010 19:11
yeah, i just noticed i don't need two loops. i changed that. the problem is, this line: tg_index = find(unique_tgs = mod); throws the error ??? Undefined function or method 'eq' for input arguments of type 'cell'. in fact, all the data structures i'm working with are cells (data, mod, tgs...everything). is there a cell equivalent to find? |