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

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!

 Réponse acceptée

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.

Plus de réponses (1)

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 commentaires

btw, the solution you've shown is for [1 2;5 6] and not for [1 2;3 4]
yes, last m shown for b=[1 2; 5 6]
It's right!
But I need for 0 elements of b represent as zero matrix
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.
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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Sparse Matrices dans Centre d'aide et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by