How can I expand a matrix by replacing its elements?
Afficher commentaires plus anciens
%mod p multiplication%
p=5;
a=0:p-1;
b=repmat(a,p,1);
s=mod(b.*b',p);
the above code generates the matrix s as
s=[0 0 0 0 0;0 1 2 3 4;0 2 4 1 3;0 3 1 4 2;0 4 3 2 1]
Now i need to expand the matrix s of size 5 by 5 to a matrix of size 5 by 25 by replacing the elements in it as:
0 as 10000,
1 as 01000,
2 as 00100,
3 as 00010,
4 as 00001.
so that the new expanded matrix is:
[10000 10000 10000 10000 10000
10000 01000 00100 00010 00001
10000 00100 00001 01000 00010
10000 00010 01000 00001 00100
10000 00001 00010 00100 01000]
I tried doing it using reshape and re-size of matrix but failed to do it.
Is there any way to expand it?
Thank you.
1 commentaire
The resulting matrix does not look like a 5 by 25 matrix. Do you mean:
[1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0; ...
etc
or:
{'10000', '10000', '10000', '10000', '10000'; ...
?
Réponse acceptée
Plus de réponses (4)
Azzi Abdelmalek
le 26 Août 2013
out=arrayfun(@(x) circshift('10000',[0 x]),s,'un',0)
1 commentaire
Pragathi
le 27 Août 2013
Modifié(e) : Azzi Abdelmalek
le 27 Août 2013
Andrei Bobrov
le 27 Août 2013
Modifié(e) : Andrei Bobrov
le 27 Août 2013
q = num2cell(eye(5),2);
out = cell2mat(q(s+1));
or
p=5;
a=0:p-1;
s = rem(a'*a,p) + 1;
ii = bsxfun(@plus,s,p*a);
jj= ndgrid(a+1);
out = zeros(p,p^2);
out(sub2ind([p,p^2],jj,ii)) = 1;
Osama AlThahab
le 26 Août 2013
Modifié(e) : Azzi Abdelmalek
le 26 Août 2013
the matrix for example is still 5*5 matrix, and you can replace the numbers 0,1,2,3,4, by making a program with (if statement) like
%mod p multiplication%
for i=1:5
for j=1:5
if a(i,j)=0
a(i,j)=10000
else if a(i,j)=1
a(i,j)=01000
.
.
.
.
end
end
end
1 commentaire
Jan
le 27 Août 2013
What do you mean by "a(i,j)=01000"? Leading zeros are not meaningful in Matlab for numbers.
Azzi Abdelmalek
le 26 Août 2013
p=5;
a=0:p-1;
b=repmat(a,p,1);
s=mod(b.*b',p);
s1=arrayfun(@num2str,s,'un',0)
s1=strrep(s1,'0','x0')
s1=strrep(s1,'1','x1')
s1=strrep(s1,'4','00001');
s1=strrep(s1,'3','00010');
s1=strrep(s1,'2','00100');
s1=strrep(s1,'x1','01000')
s1=strrep(s1,'x0','10000')
Catégories
En savoir plus sur Characters and Strings dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!