来自坐标向量的二维逻辑矩阵(Basic matlab) [英] 2D logical matrix from vector of coordinates (Basic matlab)

查看:127
本文介绍了来自坐标向量的二维逻辑矩阵(Basic matlab)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个2D矢量,其中包含我想要表示为True的坐标或一个尺寸为nxm的矩阵中的坐标。我可以在没有循环的情况下构建此矩阵吗

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?

目前我这样做:

points = [(1,1), (30, 20), (8,7)]
grid = zeros(n,m);

for i = 1:length(points)
    grid(points(i,1),points(i,2))=1;
end

非常感谢我是matlab的新手,我找不到回答到目前为止。

Thanks a lot I'm newbie in matlab and I couldn't find the answer so far.

推荐答案

你对点的赋值在语法上是不正确的,它应该是:

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

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

问题的解决方案在于使用 sub2ind <将下标转换为线性索引/ code>:

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

grid(sub2ind(size(grid),points(1,:),points(2,:)))=1

这篇关于来自坐标向量的二维逻辑矩阵(Basic matlab)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆