How to create the definite cycle and to display the result of a cycle in a single matrix of a definite size?

1 view (last 30 days)
Hello, I have cycle to represents b as matrix.
m=[];
b=[4];
for i=0:6 %Creating cycle
b=circshift(b,[0,i]); %Shift b
z=zeros(1,7,'int8'); %Create zeros string to represents b as 0 0 0...b..0
if b==0 %If b=0 then z=1 0 0...0
z(1)=1;
m=[m; z];
else
z(b)=b;
b=mod(b,7);
b=b+1;
m=[m; z];
end
end
ANSWER:
m =
0 0 0 4 0 0 0
0 0 0 0 5 0 0
0 0 0 0 0 6 0
0 0 0 0 0 0 7
1 0 0 0 0 0 0
0 2 0 0 0 0 0
0 0 3 0 0 0 0
I need to create the cycle for represent matrix b, for example, b=[1 2; 5 6] . In the form matrix m dimension 7*2x7*2=14x14. Only necessary that each value of b displayed by its matrix m For example, for b=[1 2; 3 4], I need create the following m:
m =
1 0 0 0 0 0 0 0 2 0 0 0 0 0
0 2 0 0 0 0 0 0 0 3 0 0 0 0
0 0 3 0 0 0 0 0 0 0 4 0 0 0
0 0 0 4 0 0 0 0 0 0 0 5 0 0
0 0 0 0 5 0 0 0 0 0 0 0 6 0
0 0 0 0 0 6 0 0 0 0 0 0 0 7
0 0 0 0 0 0 7 1 0 0 0 0 0 0
0 0 0 0 5 0 0 0 0 0 0 0 6 0
0 0 0 0 0 6 0 0 0 0 0 0 0 7
0 0 0 0 0 0 7 1 0 0 0 0 0 0
1 0 0 0 0 0 0 0 2 0 0 0 0 0
0 2 0 0 0 0 0 0 0 3 0 0 0 0
0 0 3 0 0 0 0 0 0 0 4 0 0 0
0 0 0 4 0 0 0 0 0 0 0 5 0 0
Help me, please!

Accepted Answer

Amit
Amit on 22 Jan 2014
Edited: Amit on 22 Jan 2014
A = eye(7).*repmat(1:7,7,1);
[x,y] = size(b);
m = zeros(7*x,7*y);
for i = 1:x
for j = 1:y
if (b(i,j) == 0)
m((i-1)*7+1:i*7,(j-1)*7+1:j*7) = 0;
else
m((i-1)*7+1:i*7,(j-1)*7+1:j*7) = circshift(A,(-1)*b(i,j)+1);
end
end
end
This will do it.

More Answers (1)

Amit
Amit on 22 Jan 2014
Here is a simpler way to do this:
A = eye(7).*repmat(1:7,7,1);
[x,y] = size(b);
m = zeros(7*x,7*y);
for i = 1:x
for j = 1:y
m((i-1)*7+1:i*7,(j-1)*7+1:j*7) = circshift(A,(-1)*b(i,j)+1);
end
end
  4 Comments
Amit
Amit on 22 Jan 2014
A = eye(7).*repmat(1:7,7,1);
[x,y] = size(b);
m = zeros(7*x,7*y);
for i = 1:x
for j = 1:y
m((i-1)*7+1:i*7,(j-1)*7+1:j*7) = circshift(A,(-1)*b(i,j));
end
end
if this is not what you want, please post an example with atleast 1 element of b being 0.
Meffix
Meffix on 22 Jan 2014
for example b=[0 2; 5 6]
m =
0 0 0 0 0 0 0 0 2 0 0 0 0 0
0 0 0 0 0 0 0 0 0 3 0 0 0 0
0 0 0 0 0 0 0 0 0 0 4 0 0 0
0 0 0 0 0 0 0 0 0 0 0 5 0 0
0 0 0 0 0 0 0 0 0 0 0 0 6 0
0 0 0 0 0 0 0 0 0 0 0 0 0 7
0 0 0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 0 5 0 0 0 0 0 0 0 6 0
0 0 0 0 0 6 0 0 0 0 0 0 0 7
0 0 0 0 0 0 7 1 0 0 0 0 0 0
1 0 0 0 0 0 0 0 2 0 0 0 0 0
0 2 0 0 0 0 0 0 0 3 0 0 0 0
0 0 3 0 0 0 0 0 0 0 4 0 0 0
0 0 0 4 0 0 0 0 0 0 0 5 0 0

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!