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:
Matrix generation

Subject: Matrix generation

From: Milos Milenkovic

Date: 8 Feb, 2014 19:09:08

Message: 1 of 18

Dear, how to generate a matrix A11 which is a combination of matrices:
  N=6 (for example)
A12=zeros(N-1, 2*N*(N-1));
for i=1:N-1
    A12(i, ((N+(i-1))*(N-1)+N-(N-0))+1)=1;
    [r,c] = find(A12>0);
end
   A13=zeros(N-1, 2*N*(N-1));
for i=1:N-1
     A13(i, ((N+(i-1))*(N-1)+N-(N-1))+1)=1;
    [r,c] = find(A13>0);
end
 A14=zeros(N-1, 2*N*(N-1));
for i=1:N-1
     A14(i, ((N+(i-1))*(N-1)+N-(N-2))+1)=1;
    [r,c] = find(A14>0);
end
 A15=zeros(N-1, 2*N*(N-1));
for i=1:N-1
     A15(i, ((N+(i-1))*(N-1)+N-(N-3))+1)=1;
    [r,c] = find(A15>0);
end
 A16=zeros(N-1, 2*N*(N-1));
for i=1:N-1
     A16(i, ((N+(i-1))*(N-1)+N-(N-4))+1)=1;
    [r,c] = find(A16>0);
end
%end

A11=[A12; A13; A14; A15; A16];

on some more compact way like:

A11=zeros((N-1)*(N-1), 2*N*(N-1));
for i=1:N-1:(N-1)*(N-1) %N-1 matrices with N-1 rows
for j=1:N-1
    for k=1:N-1
             A11(i, (N+(k-1))*(N-1)+N-(N-(j-1))+1)=1;
end
end
end

Best,
M

Subject: Matrix generation

From: dpb

Date: 9 Feb, 2014 15:56:03

Message: 2 of 18

On 2/8/2014 1:09 PM, Milos Milenkovic wrote:
...

> on some more compact way like:
>
> A11=zeros((N-1)*(N-1), 2*N*(N-1));
> for i=1:N-1:(N-1)*(N-1) %N-1 matrices with N-1 rows
> for j=1:N-1 for k=1:N-1
> A11(i, (N+(k-1))*(N-1)+N-(N-(j-1))+1)=1; end
> end
> end

L=(N-1)^2;
A=zeros(L, 2*N*(N-1));
for i=1:N-1:size(A,1)
   A(i,N+L:N+2*L-1)=ones(1,L);
end

--

Subject: Matrix generation

From: Milos Milenkovic

Date: 9 Feb, 2014 18:12:08

Message: 3 of 18

dpb <none@non.net> wrote in message <ld88en$ofp$1@speranza.aioe.org>...
> On 2/8/2014 1:09 PM, Milos Milenkovic wrote:
> ...
>
> > on some more compact way like:
> >
> > A11=zeros((N-1)*(N-1), 2*N*(N-1));
> > for i=1:N-1:(N-1)*(N-1) %N-1 matrices with N-1 rows
> > for j=1:N-1 for k=1:N-1
> > A11(i, (N+(k-1))*(N-1)+N-(N-(j-1))+1)=1; end
> > end
> > end
>
> L=(N-1)^2;
> A=zeros(L, 2*N*(N-1));
> for i=1:N-1:size(A,1)
> A(i,N+L:N+2*L-1)=ones(1,L);
> end
>
> --
>
Thank you, it is not correct, but I think it is very close.

Subject: Matrix generation

From: dpb

Date: 9 Feb, 2014 21:03:37

Message: 4 of 18

On 2/9/2014 12:12 PM, Milos Milenkovic wrote:
...

> Thank you, it is not correct, but I think it is very close.

all(a11(:)==a(:))

returned 1 here for N=3,4,5 where a11 was your result and a was mine...

 >> N=3;
 >> A11=zeros((N-1)*(N-1), 2*N*(N-1));
 >> for i=1:N-1:(N-1)*(N-1) %N-1 matrices with N-1 rows
  for j=1:N-1 for k=1:N-1
  A11(i, (N+(k-1))*(N-1)+N-(N-(j-1))+1)=1; end
  end, end
 >>

 >> L=(N-1)^2;
 >> A=zeros(L, 2*N*(N-1));
 >> for i=1:N-1:size(A,1); A(i,N+L:N+2*L-1)=ones(1,L);end
 >> all(A(:)==A11(:))
ans =
      1

OK for N==3...

 >> N=4;
 >> A11=zeros((N-1)*(N-1), 2*N*(N-1));
 >> for i=1:N-1:(N-1)*(N-1) %N-1 matrices with N-1 rows
  for j=1:N-1 for k=1:N-1
  A11(i, (N+(k-1))*(N-1)+N-(N-(j-1))+1)=1; end
  end, end
 >>
 >> L=(N-1)^2;
 >> A=zeros(L, 2*N*(N-1));
 >> for i=1:N-1:size(A,1);A(i,N+L:N+2*L-1)=ones(1,L);end
 >> all(A(:)==A11(:))
ans =
      1
 >>

QED for N=4,5...

--

Subject: Matrix generation

From: dpb

Date: 10 Feb, 2014 01:21:35

Message: 5 of 18

On 2/9/2014 3:03 PM, dpb wrote:
...


> QED for N=4,5...

ERRATUM: N=3,4, ...

--

Subject: Matrix generation

From: Milos Milenkovic

Date: 10 Feb, 2014 09:54:10

Message: 6 of 18

Dear dpb,
very sorry, my mistake, I thought my alternative code is a non working, I presented it as an idea how to make the first one shorter. In the mid time I expanded the first version and it looks like this:

A11=zeros((N-1)*(N-1), 2*N*(N-1));
A12=zeros(N-1, 2*N*(N-1));
for i=1:N-1
    A12(i, ((N+(i-1))*(N-1)+N-(N-0))+1)=1; %0 in the bracket is first value of a changing variable which goes from N-1:N+1
      [r,c] = find(A12>0);
end
A12(2:N-1,:)=circshift(A12(2:N-1,:),[1 N]); %2 in the bracket is first value of a changing variable which goes from N+1:N-1
 [r,c] = find(A12>0);
 
A13=zeros(N-1, 2*N*(N-1));
for i=1:N-1
     A13(i, ((N+(i-1))*(N-1)+N-(N-1))+1)=1;
    [r,c] = find(A13>0);
end
A13(3:N-1,:)=circshift(A13(3:N-1,:),[1 N]);
[r,c] = find(A13>0);

A14=zeros(N-1, 2*N*(N-1));
for i=1:N-1
     A14(i, ((N+(i-1))*(N-1)+N-(N-2))+1)=1;
    [r,c] = find(A14>0);
end
A14(4:N-1,:)=circshift(A14(4:N-1,:),[1 N]);
[r,c] = find(A14>0);

A15=zeros(N-1, 2*N*(N-1));
for i=1:N-1
    A15(i, ((N+(i-1))*(N-1)+N-(N-3))+1)=1;
   [r,c] = find(A15>0);
end
A15(5:N-1,:)=circshift(A15(5:N-1,:),[1 N]);
[r,c] = find(A15>0);

 A16=zeros(N-1, 2*N*(N-1));
for i=1:N-1
     A16(i, ((N+(i-1))*(N-1)+N-(N-4))+1)=1;
    [r,c] = find(A16>0);
end
A11=[A12; A13; A14; A15; A16];

This is the problem, how to obtain A11 on a more compact and general way (all as a function of N).

Best,
M

Subject: Matrix generation

From: Milos Milenkovic

Date: 10 Feb, 2014 10:26:08

Message: 7 of 18


Dear dpb,
very sorry, my mistake, I thought my alternative code is a non working, I presented it as an idea how to make the first one shorter. In the mid time I expanded the first version and it looks like this:

A11=zeros((N-1)*(N-1), 2*N*(N-1));
A12=zeros(N-1, 2*N*(N-1));
for i=1:N-1
    A12(i, ((N+(i-1))*(N-1)+N-(N-0))+1)=1; %0 in the bracket is first value of a changing variable which goes from 0:N-2
      [r,c] = find(A12>0);
end
A12(2:N-1,:)=circshift(A12(2:N-1,:),[1 N]); %2 in the bracket is first value of a changing variable which goes from 2:N-1
 [r,c] = find(A12>0);
 
A13=zeros(N-1, 2*N*(N-1));
for i=1:N-1
     A13(i, ((N+(i-1))*(N-1)+N-(N-1))+1)=1;
    [r,c] = find(A13>0);
end
A13(3:N-1,:)=circshift(A13(3:N-1,:),[1 N]);
[r,c] = find(A13>0);

A14=zeros(N-1, 2*N*(N-1));
for i=1:N-1
     A14(i, ((N+(i-1))*(N-1)+N-(N-2))+1)=1;
    [r,c] = find(A14>0);
end
A14(4:N-1,:)=circshift(A14(4:N-1,:),[1 N]);
[r,c] = find(A14>0);

A15=zeros(N-1, 2*N*(N-1));
for i=1:N-1
    A15(i, ((N+(i-1))*(N-1)+N-(N-3))+1)=1;
   [r,c] = find(A15>0);
end
A15(5:N-1,:)=circshift(A15(5:N-1,:),[1 N]);
[r,c] = find(A15>0);

 A16=zeros(N-1, 2*N*(N-1));
for i=1:N-1
     A16(i, ((N+(i-1))*(N-1)+N-(N-4))+1)=1;
    [r,c] = find(A16>0);
end
A11=[A12; A13; A14; A15; A16];

This is the problem, how to obtain A11 on a more compact and general way (all as a function of N).

Best,
M

Subject: Matrix generation

From: dpb

Date: 10 Feb, 2014 14:56:54

Message: 8 of 18

On 2/10/2014 4:26 AM, Milos Milenkovic wrote:
>
> Dear dpb,
> very sorry, my mistake, I thought my alternative code is a non working,
> I presented it as an idea how to make the first one shorter. In the mid
> time I expanded the first version and it looks like this:
...

> A16=zeros(N-1, 2*N*(N-1));
> for i=1:N-1
> A16(i, ((N+(i-1))*(N-1)+N-(N-4))+1)=1; [r,c] = find(A16>0);
> end
> A11=[A12; A13; A14; A15; A16];
>
> This is the problem, how to obtain A11 on a more compact and general way
> (all as a function of N).

This doesn't work, either...

 >> N=3;
 >> milos
Error using vertcat
Dimensions of matrices being concatenated are not consistent.
Error in milos (line 30)
A11=[A12; A13; A14; A15; A16];
 >> whos A1*
   Name Size Bytes Class Attributes

   A11 4x12 384 double
   A12 2x12 192 double
   A13 2x12 192 double
   A14 2x12 192 double
   A15 2x12 192 double
   A16 2x13 208 double

Looking at the A16 indexing you're doing on finds--

 >> for i=1:N-1
disp([i, ((N+(i-1))*(N-1)+N-(N-4))+1]),end
      1 11
      2 13
 >>

--

Subject: Matrix generation

From: Milos Milenkovic

Date: 10 Feb, 2014 16:30:12

Message: 9 of 18

Dear dpb,
yes N-4 for N=3 makes problem, but that is according to the fact that the number of matrices (A12...) is (N-1),I need to define that in code, but step by step. So, for N=3 I will have A12 and A13 as components of A11, for N=4 - A12,A13,A14, for N=5 - A12-A15 etc.

Subject: Matrix generation

From: dpb

Date: 10 Feb, 2014 17:54:10

Message: 10 of 18

On 2/10/2014 10:30 AM, Milos Milenkovic wrote:
> Dear dpb,
> yes N-4 for N=3 makes problem, but that is according to the fact that
> the number of matrices (A12...) is (N-1),I need to define that in code,
> but step by step. So, for N=3 I will have A12 and A13 as components of
> A11, for N=4 - A12,A13,A14, for N=5 - A12-A15 etc.

I have no idea what the above is attempting to say.

What range of N is allowable and does the code generate the desired
result? Have to define the problem before can write a solution.

--

Subject: Matrix generation

From: dpb

Date: 10 Feb, 2014 22:21:21

Message: 11 of 18

On 2/10/2014 11:54 AM, dpb wrote:
> On 2/10/2014 10:30 AM, Milos Milenkovic wrote:
>> Dear dpb,
>> yes N-4 for N=3 makes problem, but that is according to the fact that
>> the number of matrices (A12...) is (N-1),I need to define that in code,
>> but step by step. So, for N=3 I will have A12 and A13 as components of
>> A11, for N=4 - A12,A13,A14, for N=5 - A12-A15 etc.
>
> I have no idea what the above is attempting to say.
>
> What range of N is allowable and does the code generate the desired
> result? Have to define the problem before can write a solution.

Well, let me take a stab at trying to interpret -- is N a parameter for
a given problem and the resulting matrix to be generated is of N-1
subsections? And, for a different N there is a differing-sized end result?

If so, would seem the easiest would be to pass N and build the
submatrices in a a loop as go inside the original rather than trying to
build a bunch of differently-name variables (almost always a bad idea in
Matlab).

But, an actual desired output or working example from which to
generalize would help immeasurably.

--

Subject: Matrix generation

From: Milos Milenkovic

Date: 11 Feb, 2014 08:18:12

Message: 12 of 18

%N=3
A12 =
     0 0 0 0 0 0 1 0 0 0 0 0
     0 0 0 0 0 0 0 0 0 0 0 1

A13 =
     0 0 0 0 0 0 0 1 0 0 0 0
     0 0 0 0 0 0 0 0 0 1 0 0

A11 =[A12; A13] =
     0 0 0 0 0 0 1 0 0 0 0 0
     0 0 0 0 0 0 0 0 0 0 0 1
     0 0 0 0 0 0 0 1 0 0 0 0
     0 0 0 0 0 0 0 0 0 1 0 0
     
     %N=4
   A12 =
  Columns 1 through 17
     0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
     0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  Columns 18 through 24
     0 0 0 0 0 0 0
     0 0 0 0 0 1 0
     0 0 1 0 0 0 0
A13 =
  Columns 1 through 17
     0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
     0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
     0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  Columns 18 through 24
     0 0 0 0 0 0 0
     0 0 0 0 0 0 0
     0 0 0 0 0 0 1
A14 =
  Columns 1 through 17
     0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
     0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  Columns 18 through 24
     0 0 0 0 0 0 0
     1 0 0 0 0 0 0
     0 0 0 1 0 0 0
A11 = [A12; A13; A14] =
  Columns 1 through 17
     0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
     0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
     0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
     0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  Columns 18 through 24
     0 0 0 0 0 0 0
     0 0 0 0 0 1 0
     0 0 1 0 0 0 0
     0 0 0 0 0 0 0
     0 0 0 0 0 0 0
     0 0 0 0 0 0 1
%For N=25, A11 will have [(N-1)*(N-1),2*N*(N-1)], and be composed from A12 (from 1-N-1 of A11), A13 (from N-2*(N-1) of A11), A14,...,A35(from (N-2)*(N-1)+1 - (N-1)*(N-1)) which will
%be composed according to the following law:
A12=zeros(N-1, 2*N*(N-1));
for i=1:N-1
    A12(i, ((N+(i-1))*(N-1)+N-(N-0))+1)=1;
      [r,c] = find(A12>0);
end
A12(2:N-1,:)=circshift(A12(2:N-1,:),[1 N]);
 [r,c] = find(A12>0);

 A13=zeros(N-1, 2*N*(N-1));
for i=1:N-1
     A13(i, ((N+(i-1))*(N-1)+N-(N-1))+1)=1;
    [r,c] = find(A13>0);
end
A13(3:N-1,:)=circshift(A13(3:N-1,:),[1 N]);
[r,c] = find(A13>0);

A14=zeros(N-1, 2*N*(N-1));
for i=1:N-1
     A14(i, ((N+(i-1))*(N-1)+N-(N-2))+1)=1;
    [r,c] = find(A14>0);
end
A14(4:N-1,:)=circshift(A14(4:N-1,:),[1 N]);
[r,c] = find(A14>0);
%....
A35... %THERE IS NO CIRCSHIFT FOR THE LAST MATRIX IN A11 (So for N=3 there is no circshift in A13)

%These matrices are all for A11 with N=25 (24 matrices)
%Or for N=n on more compact way (not correct, just the essence of idea)
k=0:N-2;
z=2:N-1;
j=1:N-1; N:2*(N-1);...;(N-2)*(N-1)+1:(N-1)*(N-1)
    A11(j,((N+(j-1))*(N-1)+N-(N-k))+1)=1;
A11(z:N-1,:)=circshift(A11(z:N-1,:),[1,N])
end
end
end

%Please retain this form of program (for loops)

Subject: Matrix generation

From: dpb

Date: 11 Feb, 2014 20:38:56

Message: 13 of 18

On 2/11/2014 2:18 AM, Milos Milenkovic wrote:
> %be composed according to the following law:
> A12=zeros(N-1, 2*N*(N-1));
> for i=1:N-1
> A12(i, ((N+(i-1))*(N-1)+N-(N-0))+1)=1;
...
> A13(i, ((N+(i-1))*(N-1)+N-(N-1))+1)=1;
...
> A14(i, ((N+(i-1))*(N-1)+N-(N-2))+1)=1;
...

OK, you just need to do the algebraic simplification and generalization
to compute the indices to load for each group...

((N+(i-1))*(N-1)+N-(N-0))+1
((N+(i-1))*(N-1)+N-(N-1))+1
((N+(i-1))*(N-1)+N-(N-2))+1

...various simplification steps...

(N+i-1)*(N-1)+1
(N+i-1)*(N-1)+2
(N+i-1)*(N-1)+3

Looking at each column computation in order we find

(N-1+i)*(N-1)+j

where j is the matrix number from 1:N-1 (your second subscript 2,3,4,...
above but starting from 1 instead of 2).

So, with that, to build the array simply build the indices vectors; the
row is

ix=1:(N-1)^2

as you've identified, the column is then

ij (N-1+[1:N-1])*(N-1)+j for each group.

As example, for N=3 we get

 >> jx=[];for j=1:N-1,jx=[jx (N-1+[1:N-1])*(N-1)+j];end
 >> jx
jx =
      7 9 8 10
 >>

Then to set the locations in A to one, simply use the two vectors in
ind2sub...

 >> A(sub2ind(size(A),ix',jx'))=1
A =
      0 0 0 0 0 0 1 0 0 0 0 0
      0 0 0 0 0 0 0 0 1 0 0 0
      0 0 0 0 0 0 0 1 0 0 0 0
      0 0 0 0 0 0 0 0 0 1 0 0
 >>

I'll leave you to generalize the circshift() operations.

--

Subject: Matrix generation

From: dpb

Date: 11 Feb, 2014 21:30:39

Message: 14 of 18

On 2/11/2014 2:38 PM, dpb wrote:
...

> >> A(sub2ind(size(A),ix',jx'))=1
...

> I'll leave you to generalize the circshift() operations.

On reflection, seems like it _might_ be simpler to just compute the
locations directly reflecting the result of the pending circshift() and
avoid that operation entirely.

Again, I'll leave that as an "exercise for the student" but as a thought
to consider anyway... :)

--

Subject: Matrix generation

From: Milos Milenkovic

Date: 12 Feb, 2014 12:45:08

Message: 15 of 18

dpb <none@non.net> wrote in message <lde4r4$phi$1@speranza.aioe.org>...
> On 2/11/2014 2:38 PM, dpb wrote:
> ...
>
> > >> A(sub2ind(size(A),ix',jx'))=1
> ...
>
> > I'll leave you to generalize the circshift() operations.
>
> On reflection, seems like it _might_ be simpler to just compute the
> locations directly reflecting the result of the pending circshift() and
> avoid that operation entirely.
>
> Again, I'll leave that as an "exercise for the student" but as a thought
> to consider anyway... :)
>
> --
> Dear dpb,
excellent, thank you!! Yes, I will try to incorporate circ... in your code.

Subject: Matrix generation

From: dpb

Date: 12 Feb, 2014 14:44:45

Message: 16 of 18

On 2/12/2014 6:45 AM, Milos Milenkovic wrote:
> dpb <none@non.net> wrote in message <lde4r4$phi$1@speranza.aioe.org>...
>> On 2/11/2014 2:38 PM, dpb wrote:
>> ...
>>
>> > >> A(sub2ind(size(A),ix',jx'))=1
>> ...
>>
>> > I'll leave you to generalize the circshift() operations.
>>
>> On reflection, seems like it _might_ be simpler to just compute the
>> locations directly reflecting the result of the pending circshift()
>> and avoid that operation entirely.
>>
>> Again, I'll leave that as an "exercise for the student" but as a
>> thought to consider anyway... :)
>>
>> --
>> Dear dpb,
> excellent, thank you!! Yes, I will try to incorporate circ... in your code.

Also, as you will note from the jx vector above there's a fixed
relationship between the sets there as well. More than likely a little
more consideration of the algebra and the construction than I had time
for would lead to eliminating the loop entirely for another vector
operation on j as well as i. I took the "cheap but cheery" way out once
the general idea of the algebraic reduction was demonstrated.

--

Subject: Matrix generation

From: dpb

Date: 12 Feb, 2014 19:09:20

Message: 17 of 18

On 2/12/2014 8:44 AM, dpb wrote:
> On 2/12/2014 6:45 AM, Milos Milenkovic wrote:
>> dpb <none@non.net> wrote in message <lde4r4$phi$1@speranza.aioe.org>...
>>> On 2/11/2014 2:38 PM, dpb wrote:
>>> ...
>>>
>>> > >> A(sub2ind(size(A),ix',jx'))=1
>>> ...
>>>
>>> > I'll leave you to generalize the circshift() operations.
>>>
>>> On reflection, seems like it _might_ be simpler to just compute the
>>> locations directly reflecting the result of the pending circshift()
>>> and avoid that operation entirely.
>>>
>>> Again, I'll leave that as an "exercise for the student" but as a
>>> thought to consider anyway... :)
>>>
>>> --
>>> Dear dpb,
>> excellent, thank you!! Yes, I will try to incorporate circ... in your
>> code.
>
> Also, as you will note from the jx vector above there's a fixed
> relationship between the sets there as well. More than likely a little
> more consideration of the algebra and the construction than I had time
> for would lead to eliminating the loop entirely for another vector
> operation on j as well as i. I took the "cheap but cheery" way out once
> the general idea of the algebraic reduction was demonstrated.

And, btw, while I didn't explore the characteristic change with N, it
appears that actually what you're loading initially is a subsection of a
diagonal so perhaps clever use of BLKDIAG and/or DIAG might also come
into play...

--

Subject: Matrix generation

From: Milos Milenkovic

Date: 12 Feb, 2014 20:01:11

Message: 18 of 18

Dear dpb,
this is the complete code (covering the circshift part also), maybe not very efficient but it works correctly.
Thanks once again.
N=...;
ix=1:(N-1)^2;
jx=[];
for j=1:N-1
    jx=[jx (N-1+[1:N-1])*(N-1)+j]
end
    B=(vec2mat(jx,N-1))'
    [nrows,ncols]=size(B);
    for i=1:nrows
        for j=1:ncols
    if i>j
        B(i,j)=B(i,j)+N;
    end
        end
    end
    zx=(reshape(B,(N-1)^2,1))';
     A110=zeros((N-1)*(N-1), 2*N*(N-1));
 A110(sub2ind(size(A110),ix',zx'))=1;

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