How can I expand a matrix by replacing its elements?
조회 수: 1 (최근 30일)
이전 댓글 표시
%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
채택된 답변
Jan
2013년 8월 27일
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);
댓글 수: 0
추가 답변 (4개)
Andrei Bobrov
2013년 8월 27일
편집: Andrei Bobrov
2013년 8월 27일
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;
댓글 수: 0
Osama AlThahab
2013년 8월 26일
편집: Azzi Abdelmalek
2013년 8월 26일
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
Jan
2013년 8월 27일
What do you mean by "a(i,j)=01000"? Leading zeros are not meaningful in Matlab for numbers.
Azzi Abdelmalek
2013년 8월 26일
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')
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!