Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
What is the MATLAB one-line expression...

Subject: What is the MATLAB one-line expression...

From: Jeff

Date: 29 Nov, 2009 02:24:03

Message: 1 of 14

...to create the n ? n matrix A satisfying

a(i,j) = 1, if i − j is prime
0, if i-j is not prime

This is question 2 at the end of Chapter 2 of the book Crash course in MATLAB, by Tobin Driscoll. Neither me, nor my professor can find the answer and it's killing me. Note: This is not a high priority question.

Subject: What is the MATLAB one-line expression...

From: ade77

Date: 29 Nov, 2009 02:46:03

Message: 2 of 14

"Jeff " <spREMOVEHITSjeffAT@SIGNoptonline.net> wrote in message <hesm03$q5m$1@fred.mathworks.com>...
> ...to create the n ? n matrix A satisfying
>
> a(i,j) = 1, if i − j is prime
> 0, if i-j is not prime
>
> This is question 2 at the end of Chapter 2 of the book Crash course in MATLAB, by Tobin Driscoll. Neither me, nor my professor can find the answer and it's killing me. Note: This is not a high priority question.

Post the whole question, not everyone have access to the book

Subject: What is the MATLAB one-line expression...

From: Matt Fig

Date: 29 Nov, 2009 03:10:18

Message: 3 of 14

The FOR loop solution is straight forward.

% Data
n = 9;

% FOR loop solution
a = zeros(n,n);
for ii = 1:n
    for jj = 1:ii-1
        if isprime(ii-jj)
            a(ii,jj) = 1;
        end
    end
end



But there are also many ways to vectorize this problem. Here is one.

% Vectorized.
P = primes(n);
A = repmat(ones(n,1),1,length(P));
A = spdiags(A,-P,n,n) % See also the "Full" command.

Subject: What is the MATLAB one-line expression...

From: Matt Fig

Date: 29 Nov, 2009 03:42:04

Message: 4 of 14

I just noticed that you wanted it all in one line. My previous vectorized solution could go on one line, just so you know.

Subject: What is the MATLAB one-line expression...

From: the cyclist

Date: 29 Nov, 2009 03:53:04

Message: 5 of 14

"Jeff " <spREMOVEHITSjeffAT@SIGNoptonline.net> wrote in message <hesm03$q5m$1@fred.mathworks.com>...
> ...to create the n ? n matrix A satisfying
>
> a(i,j) = 1, if i − j is prime
> 0, if i-j is not prime
>
> This is question 2 at the end of Chapter 2 of the book Crash course in MATLAB, by Tobin Driscoll. Neither me, nor my professor can find the answer and it's killing me. Note: This is not a high priority question.

Assuming that one-liner implies no semi-colons, then here is an atrocious solution. [The tricky part for me was forming the array whose (i,j)th element is i-j.]

>> a = isprime(max(1,cumsum(cumsum(eye(n))-eye(n)) - cumsum(cumsum(eye(n))-eye(n))'))

the cyclist

Subject: What is the MATLAB one-line expression...

From: Matt Fig

Date: 29 Nov, 2009 03:59:03

Message: 6 of 14

This should be pretty fast.
A = full(spdiags(repmat(ones(n,1),1,length(primes(n))),-primes(n),n,n));

Here is another one that is slower.

A = tril(isprime(abs(bsxfun(@minus,repmat((1:n),n,1),(1:n).'))));

Subject: What is the MATLAB one-line expression...

From: the cyclist

Date: 29 Nov, 2009 04:07:04

Message: 7 of 14

"the cyclist" <thecyclist@gmail.com> wrote in message <hesr6v$bv5$1@fred.mathworks.com>...
> "Jeff " <spREMOVEHITSjeffAT@SIGNoptonline.net> wrote in message <hesm03$q5m$1@fred.mathworks.com>...
> > ...to create the n ? n matrix A satisfying
> >
> > a(i,j) = 1, if i − j is prime
> > 0, if i-j is not prime
> >
> > This is question 2 at the end of Chapter 2 of the book Crash course in MATLAB, by Tobin Driscoll. Neither me, nor my professor can find the answer and it's killing me. Note: This is not a high priority question.
>
> Assuming that one-liner implies no semi-colons, then here is an atrocious solution. [The tricky part for me was forming the array whose (i,j)th element is i-j.]
>
> >> a = isprime(max(1,cumsum(cumsum(eye(n))-eye(n)) - cumsum(cumsum(eye(n))-eye(n))'))
>
> the cyclist

Shorter, and a bit more elegant:

>> a = isprime(max(1,1./gallery('cauchy',1:n,-(1:n))))

the cyclist

Subject: What is the MATLAB one-line expression...

From: Matt Fig

Date: 29 Nov, 2009 04:10:20

Message: 8 of 14

Here's another, inspired by the cyclist's solution:

b = isprime(cumsum(tril(ones(n)) - eye(n)))

Subject: What is the MATLAB one-line expression...

From: Jeff

Date: 29 Nov, 2009 04:16:04

Message: 9 of 14

"ade77 " <ade100a@gmail.com> wrote in message <hesn9b$ed9$1@fred.mathworks.com>...
> "Jeff " <spREMOVEHITSjeffAT@SIGNoptonline.net> wrote in message <hesm03$q5m$1@fred.mathworks.com>...
> > ...to create the n ? n matrix A satisfying
> >
> > a(i,j) = 1, if i − j is prime
> > 0, if i-j is not prime
> >
> > This is question 2 at the end of Chapter 2 of the book Crash course in MATLAB, by Tobin Driscoll. Neither me, nor my professor can find the answer and it's killing me. Note: This is not a high priority question.
>
> Post the whole question, not everyone have access to the book

Actually, that was the whole question. It was a short question. The whole point was to teach us to use Matlab's colon notation (and maybe other shortcuts, too). The exact wording (copy and pasted this time) is:

Find a MATLAB one-line expression to create the n ? n matrix A satisfying
a(i,j) =
(1, if i − j is prime
(0, otherwise

Subject: What is the MATLAB one-line expression...

From: Matt Fig

Date: 29 Nov, 2009 04:18:00

Message: 10 of 14

And another (these are fun!), actually this is a modification of a previous one.

T = full(spdiags(ones(n),-primes(n),n,n));

Subject: What is the MATLAB one-line expression...

From: Jeff

Date: 29 Nov, 2009 05:01:17

Message: 11 of 14

"Matt Fig" <spamanon@yahoo.com> wrote in message <hesslo$avq$1@fred.mathworks.com>...
> And another (these are fun!), actually this is a modification of a previous one.
>
> T = full(spdiags(ones(n),-primes(n),n,n));

It seemed like a fun problem, that's why I was curious about how to do it.

Anyway, thanks all for the great solutions (one or two of which I'm still trying to understand). Not only were they fun, they were edifying.

Subject: What is the MATLAB one-line expression...

From: the cyclist

Date: 29 Nov, 2009 15:01:03

Message: 12 of 14

"Jeff " <spREMOVEHITSjeffAT@SIGNoptonline.net> wrote in message <hesv6t$ejo$1@fred.mathworks.com>...
> "Matt Fig" <spamanon@yahoo.com> wrote in message <hesslo$avq$1@fred.mathworks.com>...
> > And another (these are fun!), actually this is a modification of a previous one.
> >
> > T = full(spdiags(ones(n),-primes(n),n,n));
>
> It seemed like a fun problem, that's why I was curious about how to do it.
>
> Anyway, thanks all for the great solutions (one or two of which I'm still trying to understand). Not only were they fun, they were edifying.

This is reminiscent of the Matlab Golf contest a few years back, where one had to try to find the solution to a particular challenge in the fewest possible characters. Here's the shortest one so far:

>> b=isprime(cumsum(tril(ones(n))-eye(n)))

Is a shorter solution possible?

Subject: What is the MATLAB one-line expression...

From: Bruno Luong

Date: 29 Nov, 2009 15:57:00

Message: 13 of 14

>
> >> b=isprime(cumsum(tril(ones(n))-eye(n)))
>
> Is a shorter solution possible?

no!
b=isprime(tril(toeplitz(0:n-1)))

Bruno

Subject: What is the MATLAB one-line expression...

From: Bruno Luong

Date: 29 Nov, 2009 17:32:19

Message: 14 of 14

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message
> >
> > Is a shorter solution possible?
>
> Yes!
> b=isprime(tril(toeplitz(0:n-1)))
>
> Bruno

Sorry I sent out the above reply in a hurry, and it was non-sense. That Matlab code makes sense however. LOL

Bruno

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us