Thursday 15 July 2010

matlab - Retrieve column and row elements based on single element -



matlab - Retrieve column and row elements based on single element -

given m x n matrix, how can obtain ordered (top-left bottom-right) entries column , row corresponding given index without indexed element itself?

for example, given 5 x 5 magic square matrix i'd retrieve column , row elements corresponding (4,2) element:

a = 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9

this should yield:

b = 24 5 6 10 19 21 3 18

alternatively, requesting (5,5) element yield:

b = 15 16 22 3 11 18 25 2

or, (3,2) we'd have:

b = 24 5 4 13 20 22 12 18

if order of elements in output b isn't of import can utilize -

b = setdiff([a(:,col_id).' a(row_id,:)],a(row_id,col_id),'stable')

if order important, messy solution looks fit -

b = [a(1:row_id-1,col_id).' a(row_id,1:col_id-1) ... a(row_id,col_id+1:end) a(row_id+1:end,col_id).']

matlab

No comments:

Post a Comment