フィルターのクリア

How to draw a line on a logical matrix?

3 ビュー (過去 30 日間)
John Wray
John Wray 2016 年 5 月 19 日
編集済み: Andrei Bobrov 2016 年 5 月 19 日
Hi,
Assume a blank matrix:
m=[0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0]
I want to draw a line between a=[1 1], b=[5 5], and returned m like this:
m = [1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1]

回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2016 年 5 月 19 日
編集済み: Andrei Bobrov 2016 年 5 月 19 日
diag(5)
or
z = zeros(6.,7);
a=[2 1];
b=[4 7];
xy = [a; b];
t = diff(xy);
t0 = t(:,1)./t(:,2);
y = @(x)(x - a(2))*t0 + a(1);
x1 = (1:size(z,2))';
y1 = round(y(x1));
z(size(z,1)*(x1 - 1) + y1) = 1
or use strel from Image Processing Toolbox
z = zeros(6.,7);
a=[2 1];
b=[4 7];
t = diff([a; b]);
S = strel('line',sqrt(sum(t.^2)),atand(t(1)/-t(2)));
  1 件のコメント
John Wray
John Wray 2016 年 5 月 19 日
Hi,
thanks for your answer, I mean not to create a diag matrix, the two points can be any value,.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeElementary Math についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by