フィルターのクリア

If I start with a matrix of zeros, how can I easily create a triangle of ones in that matrix?

2 ビュー (過去 30 日間)
I want to be able to get a .mat file that will create a filled triangle when I use imagesc. I have been told the easiest way is to start with a matrix full of zeros and then create a triangle of ones inside of that, but I can not figure out how to do that, other than manually which would take a while.

採用された回答

KSSV
KSSV 2017 年 6 月 19 日
N = 100 ;
T1 = triu(ones(N),1) ;
T2 = fliplr(T1) ;
T = [T2 T1] ;
figure(1)
imagesc(T)
  2 件のコメント
Daniel Gray
Daniel Gray 2017 年 6 月 19 日
You are amazing! Thank you
Adam
Adam 2017 年 6 月 19 日
編集済み: Adam 2017 年 6 月 19 日
If you want an actual equilateral triangle then Andrei Bobrov's answer will create that. This triangle is not actually equilateral.

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

その他の回答 (3 件)

Andrei Bobrov
Andrei Bobrov 2017 年 6 月 19 日
N = 1000;
T = tril(ones(N)) ;
a = round(imresize([flip(T,2),T],[round(N/2*tan(pi/3)) N]));
imagesc(a)

Image Analyst
Image Analyst 2017 年 6 月 19 日
If you have the Image Processing Toolbox, it's a simple one liner:
triangleMatrix = poly2mask(x, y, rows, columns);
where x and y are 3 element arrays of the rows and columns where you want the vertices, and rows and columns are the overall size of your output matrix. Be careful not to mix up x and y with row and column - remember (x,y) = (column,row), not (row, column).
  1 件のコメント
Andrei Bobrov
Andrei Bobrov 2017 年 6 月 19 日
編集済み: Andrei Bobrov 2017 年 6 月 19 日
+1
N = 1000;
k = 1/sqrt(3);
c = round(.5*N*[1-k,4/3;1,1/3;1+k,4/3]);
a = poly2mask(c(:,1),c(:,2),N,N);
imagesc(a)

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


Adam
Adam 2017 年 6 月 19 日
It depends where you want the triangle. For example
res = tril( ones(100) );
will produce a triangle in the lower left of the matrix.
  3 件のコメント
Sophia Andaloro
Sophia Andaloro 2017 年 6 月 21 日
I'm wondering the same thing. Let me know if you figure it out.
Image Analyst
Image Analyst 2017 年 6 月 21 日
Like I said, in my answer, poly2mask() will do it, subject to quantization of course.

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

カテゴリ

Help Center および File ExchangeAxes Appearance についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by