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:
Construct a matrix with rotational column elements

Subject: Construct a matrix with rotational column elements

From: Wendy

Date: 29 Nov, 2011 18:44:08

Message: 1 of 10

Hi all,

I want to construct a matrix with rotational column elements in each row. Following is an example

 A =
     1 2 3 4
     4 1 2 3
     3 4 1 2
     2 3 4 1

Given r = [1 2 3 4], instead of constructing the matrix row by row, is there any function in Matlab for doing this automatically?

Thank you,

Wendy

Subject: Construct a matrix with rotational column elements

From: Matt J

Date: 29 Nov, 2011 19:05:12

Message: 2 of 10

"Wendy " <wlq121@gmail.com> wrote in message <jb395o$haj$1@newscl01ah.mathworks.com>...
> Hi all,
>
> I want to construct a matrix with rotational column elements in each row. Following is an example
>
> A =
> 1 2 3 4
> 4 1 2 3
> 3 4 1 2
> 2 3 4 1
>
> Given r = [1 2 3 4], instead of constructing the matrix row by row, is there any function in Matlab for doing this automatically?
==================


If the matrix is to be sparse, it would be recommendable to use this tool with the 'circ' option.


http://www.mathworks.com/matlabcentral/fileexchange/26292-regular-control-point-interpolation-matrix-with-boundary-conditions


It can also handle small examples like what you've described:

>> full(interpMatrix([1 2 3 4],1,4,1,'circ')).'

ans =

     1 2 3 4
     4 1 2 3
     3 4 1 2
     2 3 4 1

Subject: Construct a matrix with rotational column elements

From: Steven_Lord

Date: 29 Nov, 2011 19:51:42

Message: 3 of 10



"Wendy " <wlq121@gmail.com> wrote in message
news:jb395o$haj$1@newscl01ah.mathworks.com...
> Hi all,
>
> I want to construct a matrix with rotational column elements in each row.
> Following is an example
>
> A =
> 1 2 3 4
> 4 1 2 3
> 3 4 1 2
> 2 3 4 1
>
> Given r = [1 2 3 4], instead of constructing the matrix row by row, is
> there any function in Matlab for doing this automatically?

r = 1:4;
A = gallery('circul', r)

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Construct a matrix with rotational column elements

From: Matt J

Date: 29 Nov, 2011 20:43:08

Message: 4 of 10

"Steven_Lord" <slord@mathworks.com> wrote in message <jb3d4e$27u$1@newscl01ah.mathworks.com>...
>
> >
> > Given r = [1 2 3 4], instead of constructing the matrix row by row, is
> > there any function in Matlab for doing this automatically?
>
> r = 1:4;
> A = gallery('circul', r)
====================


@Wendy,
Just be mindful, though, that GALLERY may not be the best thing to use for large sparse matrices. Compare:


r=[1 2 3 4 sparse(zeros(1,10000))];

%% Using interpMatrix

tic; A=interpMatrix(r,1,length(r),1,'circ').'; toc
%Elapsed time is 0.029751 seconds.


%% Using GALLERY

 tic; A=gallery('circul',r); toc
Out of memory. Type HELP MEMORY for your options.

Error in toeplitz (line 36)
t = cidx(:,ones(p,1)) + ridx(ones(m,1),:); % Toeplitz subscripts

Error in circul (line 28)
C = toeplitz( [ v(1) v(n:-1:2) ], v );

Error in gallery (line 233)
[varargout{1:max(nargout,1)}] = F(varargin{:});

Subject: Construct a matrix with rotational column elements

From: Roger Stafford

Date: 30 Nov, 2011 02:41:08

Message: 5 of 10

"Wendy " <wlq121@gmail.com> wrote in message <jb395o$haj$1@newscl01ah.mathworks.com>...
> I want to construct a matrix with rotational column elements in each row. Following is an example
>
> A =
> 1 2 3 4
> 4 1 2 3
> 3 4 1 2
> 2 3 4 1
>
> Given r = [1 2 3 4], instead of constructing the matrix row by row, is there any function in Matlab for doing this automatically?
- - - - - - - -
  Also you could use:

 A = toeplitz(circ(r(end:-1:1),1),r);

Roger Stafford

Subject: Construct a matrix with rotational column elements

From: Roger Stafford

Date: 30 Nov, 2011 05:36:08

Message: 6 of 10

"Wendy " <wlq121@gmail.com> wrote in message <jb395o$haj$1@newscl01ah.mathworks.com>...
> I want to construct a matrix with rotational column elements in each row. Following is an example
>
> A =
> 1 2 3 4
> 4 1 2 3
> 3 4 1 2
> 2 3 4 1
>
> Given r = [1 2 3 4], instead of constructing the matrix row by row, is there any function in Matlab for doing this automatically?
- - - - - - - - -
  If you're interested in variety, here's another way:

 n = length(r);
 A = reshape(r(mod(floor((0:n^2-n+1:(n^2-n+1)*(n^2-1))/n),n)+1),n,n);

Roger Stafford

Subject: Construct a matrix with rotational column elements

From: Matt J

Date: 30 Nov, 2011 06:25:09

Message: 7 of 10

"Roger Stafford" wrote in message <jb4544$ha7$1@newscl01ah.mathworks.com>...
>
> Also you could use:
>
> A = toeplitz(circ(r(end:-1:1),1),r);
=====================

It might be good to say where to obtain the CIRC function,

>> A = toeplitz(circ(r(end:-1:1),1),r);
Undefined function 'circ' for input arguments of type 'double'.

Subject: Construct a matrix with rotational column elements

From: Matt J

Date: 30 Nov, 2011 06:31:09

Message: 8 of 10

"Roger Stafford" wrote in message <jb4fc8$gf9$1@newscl01ah.mathworks.com>...
>
> If you're interested in variety, here's another way:
>
> n = length(r);
> A = reshape(r(mod(floor((0:n^2-n+1:(n^2-n+1)*(n^2-1))/n),n)+1),n,n);
=====================

But this one too can be a problem for large sparse matrices,

>> r=[1 2 3 4 sparse(zeros(1,10000))];
>> n = length(r);
 A = reshape(r(mod(floor((0:n^2-n+1:(n^2-n+1)*(n^2-1))/n),n)+1),n,n);
Error using :
Out of memory. Type HELP MEMORY for your options.

Subject: Construct a matrix with rotational column elements

From: Roger Stafford

Date: 30 Nov, 2011 07:59:09

Message: 9 of 10

"Matt J" wrote in message <jb4i85$oa5$1@newscl01ah.mathworks.com>...
> >> A = toeplitz(circ(r(end:-1:1),1),r);
> Undefined function 'circ' for input arguments of type 'double'.

  Oops! It should have been

 A = toeplitz(circshift(r(end:-1:1),1),r);

> >> r=[1 2 3 4 sparse(zeros(1,10000))];
> >> n = length(r);
> A = reshape(r(mod(floor((0:n^2-n+1:(n^2-n+1)*(n^2-1))/n),n)+1),n,n);
> Error using :
> Out of memory. Type HELP MEMORY for your options.

  Well, the memory problem is presumably due to A (and the colon series) not being sparse and, accordingly, occupying memory for a few multiples of 100,080,016 double elements.

  However, a more fundamental problem is that at this size, the integer elements themselves in the colon series become inexact due to their excessively large size - that is greater than 2^53 - and can lead to incorrect results. I should have mentioned that quirk earlier.

  Using

 A = reshape(r(mod(floor((0:-n+1:(-n+1)*(n^2-1))/n),n)+1),n,n);

instead would improve the accuracy situation but will still face the same memory problem. However this latter form does err for n = 1.

  (Anyhow it was fun working out the silly thing. I have a fascination for using 'mod' and 'floor' in interesting ways like this.)

Roger Stafford

Subject: Construct a matrix with rotational column elements

From: Matt J

Date: 30 Nov, 2011 08:34:09

Message: 10 of 10

"Roger Stafford" wrote in message <jb4nod$atf$1@newscl01ah.mathworks.com>...
> "Matt J" wrote in message <jb4i85$oa5$1@newscl01ah.mathworks.com>...
> > >> A = toeplitz(circ(r(end:-1:1),1),r);
> > Undefined function 'circ' for input arguments of type 'double'.
>
> Oops! It should have been
>
> A = toeplitz(circshift(r(end:-1:1),1),r);
================

Ah, ok. That seems kind of obvious now.



> However, a more fundamental problem is that at this size, the integer elements themselves in the colon series become inexact due to their excessively large size - that is greater than 2^53 - and can lead to incorrect results. I should have mentioned that quirk earlier.
>
> Using
>
> A = reshape(r(mod(floor((0:-n+1:(-n+1)*(n^2-1))/n),n)+1),n,n);
>
> instead would improve the accuracy situation but will still face the same memory problem. However this latter form does err for n = 1.
===========


Or maybe one should just use uint64 indices?


 



> (Anyhow it was fun working out the silly thing. I have a fascination for using 'mod' and 'floor' in interesting ways like this.)
===========

Sure.

Tags for 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