creating an equal lateral triangle in matrix

조회 수: 2 (최근 30일)
Nathan Sulzer
Nathan Sulzer 2018년 11월 10일
댓글: John D'Errico 2018년 11월 10일
Is there an easy method for generating an equal lateral triangle centered and the middle of a matrix? The image attached is what I want the matrix to look like except the triangle would be made of 1s and the background zero. Thank you in advanced.

답변 (2개)

jonas
jonas 2018년 11월 10일
편집: jonas 2018년 11월 10일
You could do something like this:
poly = polyshape([1 2 1.5],[1 1 2])
plot(poly)
set(gca,'xcolor','none','ycolor','none')
F = getframe(gca)
I = imbinarize(rgb2gray(F.cdata))
imshow(~I)
change coordinates of the polygon to create your desired triangle.
  댓글 수: 1
John D'Errico
John D'Errico 2018년 11월 10일
Drat. Polyshape has not been around long enough that I think of it immediately. :)

댓글을 달려면 로그인하십시오.


John D'Errico
John D'Errico 2018년 11월 10일
Any number of ways, certainly many if I spent any amount of time to think them up. Simplest?
1. Start with meshgrid, creating sets of indices into the final array.
2. Create a polygon, that denotes the corners of the triangle.
3. Use inpolygon to indicate which points lie inside the triangle.
4. Assign the array initially uing zeros.
5. Use logical indexing to overwrite the points inside the polygon as 1.
So simple and fast, fully vectorized. But many other ways too.

카테고리

Help CenterFile Exchange에서 Images에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by