reshape a matrix and fill empty indices by zeroes

9 次查看(过去 30 天)
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