Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create this matrix in Matlab?

Tags:

matlab

I'm attempting to solve the Code Golf: Build Me an Arc problem. My solution's not bad, but I figure, there's a simpler way to do it. Does anybody know how to generate an nxn matrix like this, given n? I spent 57 characters getting it!

 3     0     0     0     2     0     0     0     1
 0     3     0     0     2     0     0     1     0
 0     0     3     0     2     0     1     0     0
 0     0     0     3     2     1     0     0     0
 4     4     4     4     8     8     8     8     8
 0     0     0     5     6     7     0     0     0
 0     0     5     0     6     0     7     0     0
 0     5     0     0     6     0     0     7     0
 5     0     0     0     6     0     0     0     7

I would like to beat some of these matrices into shape.

Update:

This is how I get it now.

%%# Create the grid
[X Y]=meshgrid(-r:r);
%%# Compute the angles in degrees
T=atan2(-Y,X)/pi*180;
%%# Get all the angles
T=T+(T<=0)*360;

As you can see, I don't need most of the entries in T.

like image 436
Jacob Avatar asked Nov 14 '22 08:11

Jacob


1 Answers

Since this is related to a Code Golf question, consider:

[X Y]=meshgrid(r:-1:-r,-r:r);
T=180+atan2(Y,X)*180/pi;

which would save you 3 characters...

like image 113
Amro Avatar answered Dec 22 '22 00:12

Amro