How can I expand a matrix by replacing its elements?

1 Ansicht (letzte 30 Tage)
Pragathi
Pragathi am 26 Aug. 2013
%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 Kommentar
Jan
Jan am 27 Aug. 2013
Bearbeitet: Jan am 27 Aug. 2013
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'; ...
?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Jan
Jan am 27 Aug. 2013
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]
List = {'10000', '01000', '00100', '00010', '00001'}
Result = List(s + 1);

Weitere Antworten (4)

Azzi Abdelmalek
Azzi Abdelmalek am 26 Aug. 2013
out=arrayfun(@(x) circshift('10000',[0 x]),s,'un',0)
  1 Kommentar
Pragathi
Pragathi am 27 Aug. 2013
Bearbeitet: Azzi Abdelmalek am 27 Aug. 2013
The code now gives result for any p.
p=7;
a=0:p-1;
b=repmat(a,p,1);
s=mod(b.*b',p);
out=arrayfun(@(x) circshift('10000',[0 x]),s,'un',0)
Thanks a lot.

Melden Sie sich an, um zu kommentieren.


Andrei Bobrov
Andrei Bobrov am 27 Aug. 2013
Bearbeitet: Andrei Bobrov am 27 Aug. 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
Osama AlThahab am 26 Aug. 2013
Bearbeitet: Azzi Abdelmalek am 26 Aug. 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 Kommentar
Jan
Jan am 27 Aug. 2013
What do you mean by "a(i,j)=01000"? Leading zeros are not meaningful in Matlab for numbers.

Melden Sie sich an, um zu kommentieren.


Azzi Abdelmalek
Azzi Abdelmalek am 26 Aug. 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')

Kategorien

Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by