wrek
wrek

Reputation: 309

regionprop on x y corrdinates object - Matlab

I have a txt file containing coordinates (x y in multiply rows). As I use readtable I get it as 2 column table. How can convert it to logical so I use on it regionprop?

Upvotes: 0

Views: 34

Answers (1)

Luis Mendo
Luis Mendo

Reputation: 112689

You can do that easily using the sparse function:

t = table([5; 2; 3; 4], [2; 3; 3; 1]); % example table with two columns
y = full(sparse(t{:,1}, t{:,2}, true)); % or full(sparse(t{:,2}, t{:,1}, true));

This gives

y =
  5×3 logical array
   0   0   0
   0   0   1
   0   0   1
   1   0   0
   0   1   0

If the table can have repeated entries, use

y = full(sparse(t{:,1}, t{:,2}, 1)); % or full(sparse(t{:,2}, t{:,1}, 1));

to get a count of the number of times each coordinate pair appears in the table; and then maybe convert to logical. This can also be done with accumarray:

y = accumarray([t{:,1} t{:,2}], 1); % or accumarray([t{:,2} t{:,1}], 1)

Upvotes: 1

Related Questions