2D logical matrix from vector of coordinates (Basic matlab)

后端 未结 2 1714
你的背包
你的背包 2021-01-15 12:36

I have a 2D vector which contains the coordinates which I want to represent as True or one in a matrix with dimensions nxm. Can I build this matrix without a loop?

相关标签:
2条回答
  • 2021-01-15 12:44

    Your assignment to points is syntactically incorrect, it should be:

    points=[1,30,8;1,20,7];
    

    The solution to you problem lies in converting subscripts to linear indices with sub2ind:

    grid(sub2ind(size(grid),points(1,:),points(2,:)))=1
    
    0 讨论(0)
  • 2021-01-15 13:04

    I recommend using a sparse matrix if the number of coordinates (length(points)) is much smaller (<10%) than n*m. This will make better use of memory and save you time.

    points=[1,30,8;1,20,7];
    grid = sparse(points(1,:), points(2,:), 1, n ,m);
    
    0 讨论(0)
提交回复
热议问题