reshape a matrix and fill empty indices by zeroes

조회 수: 10 (최근 30일)
Fatma Alnabrisi
Fatma Alnabrisi 2019년 12월 9일
편집: dpb 2019년 12월 9일
I'm woking in encryption project.
I have the key sequare matrix A for example with dimension mxm
I need to reshape the plaintext matrix P to be with same number of rows as A which is m
Now if the size of P devided by m equals an integer, this would be fine
however if it is not the new matrix P after reshaping will need more elemnets
So, how to write a function to fill the matrix P with zeroes after reshaping.
here's my code:
A = input('Enter Your key= ','t'); %key matrix mxm
sA = size (A);
sA = sA(1,1);
m = sA;
Ax = reshape (A , m , m );
disp (Ax);
x =input('Enter Your Message= ','s'); %plain text
sX = length(x);
n = round(sX/m);
if rem(sX, m) == 0
xR = reshape(x , m ,n);
else
???????????????
  댓글 수: 6
dpb
dpb 2019년 12월 9일
편집: dpb 2019년 12월 9일
Did something wrong then...
>> b=char({'fred';'flintstone'}) % sample text array 20 char mimic a small text file
b =
2×10 char array
'fred '
'flintstone'
>> [reshape(b.',1,[]) blanks(25-numel(b))] % the augmented vector intermediary
ans =
'fred flintstone '
>> reshape([reshape(b.',1,[]) blanks(25-numel(b))],5,[]) % the mxm (m=5) array
ans =
5×5 char array
'f fs '
'r lt '
'e io '
'd nn '
' te '
>>
Of course, in general the 25 is value of m*m and the 5 is m; just used a convenient number to illustrate.
dpb
dpb 2019년 12월 9일
편집: dpb 2019년 12월 9일
In addition, the above is still a char() array, dunno what you intend to do when you speak of multiplying it with another array. You talk of augmenting w/ zero; the above used blanks instead with text, char(0) is also possible, of course.
>> double(ans)
ans =
102 32 102 115 32
114 32 108 116 32
101 32 105 111 32
100 32 110 110 32
32 32 116 101 32
>>
is the numeric represenation of above...as noted, I've no klew regarding what it is you're actually trying to do here.
Oh...one other thought--
>> char(ans).'
ans =
5×5 char array
'fred '
' '
'flint'
'stone'
' '
>>
Dunno which orientation you would prefer or if it matters...you may want to transpose the previous result?

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by