5

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.

Community
  • 1
  • 1
Jacob
  • 34,255
  • 14
  • 110
  • 165
  • After copying the text to Excel and applying conditional formatting, I see the pattern. There are eight straight lines, all originating from the edge and ending at the origin, filled with the integers 1, 2, ..., 8, respectively. The first line starts at the upper-right corner, and the (n+1)th line starts at a pseudoangle of pi/4 from the nth line. – Andreas Rejbrand Jul 30 '10 at 19:42
  • Yes, it's related to the code golf link I posted. This will be used to draw sectors. – Jacob Jul 30 '10 at 19:44
  • I haven't been able to find a way to create the array using specialized arrays. However, since this is code golf: Why do you transform everything to degrees? It should be sufficient to use multiples of `pi`. In other words, you can divide all angles by 180 (or 45, if you prefer), and save lots of characters. – Jonas Aug 01 '10 at 16:45
  • 1) The input is in degrees so it saves characters using it directly instead of converting `w` and `x` (in the golf solution) to radians 2)|'45'|<|'pi/4'| – Jacob Aug 01 '10 at 21:51
  • I'm confused. When I run your code I don't get the output you show. – Geoff Aug 03 '10 at 21:53
  • @Geoff: Divide by 45 and keep the integers (let the rest be 0). – Jacob Aug 03 '10 at 21:59
  • `[X Y]=meshgrid(-r:r);T=atan2(-Y,X)/pi+2*(Y>=0)` is 47 characters, needs to be multiplied by 4. – Geoff Aug 03 '10 at 22:09

2 Answers2

1

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...

Amro
  • 123,847
  • 25
  • 243
  • 454
0

Listed in this posted is a one-liner solution code with bsxfun that saves us from using temporary variables as it internally does the expansion that meshgrid does explicitly and at the sametime give us the ability to mention a mathematical operation to be performed between the two inputs listed inside bsxfun. With these internal operations, a bsxfun based solution seems perfect for such a code-golf thing with 43 characters solution for the stated problem -

T=180+bsxfun(@atan2,[-r:r]',r:-1:-r)*180/pi
Divakar
  • 218,885
  • 19
  • 262
  • 358