How to create the definite cycle and to display the result of a cycle in a single matrix of a definite size?
Afficher commentaires plus anciens
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
Plus de réponses (1)
Amit
le 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 commentaires
Amit
le 22 Jan 2014
btw, the solution you've shown is for [1 2;5 6] and not for [1 2;3 4]
Meffix
le 22 Jan 2014
Amit
le 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
le 22 Jan 2014
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!