Reverse operation of horzcat, reshape and dec2bin code I have written.
조회 수: 5 (최근 30일)
이전 댓글 표시
Hello, I have been writing a very simple way of encoding text as part of a level 5 course I am undertaking currently. I have reached a point where I am happy with the encoded data but I have become a bit of a car crash trying to reverse the operation. The trouble is; I am a complete novice on both code and Matlab in general. My course is a communications course and not really code focused. I chose this as a project as I found is really interesting but I do seem to have underrestimated the complexity of the task somewhat.
The elements I am struggling with are:
chr = ( secret_message );
group_len = 8;
len = numel( chr );
d = rem( len, group_len );
chr = horzcat( chr, repmat( char(255), 1, group_len-d ) );
chr = reshape( chr, 8,[] );
secret_message3 = chr(:,1:195)
secret_messageF = chr(1:length(secret_message3))
binX = dec2bin(secret_messageF,8)
This takes an already encoded message and rearranges the layout before changing to binary.
Any help is greatly appreciated. I need to be able to get back to the top of this code at the ('secret_message') in its original format, which was ASCII.
Kind Regards
댓글 수: 3
DGM
2021년 6월 27일
In order to know how to reverse (e.g. reshape) operations, one needs to know what they're trying to undo. As you said yourself, the prior code could be anything. It should stand to reason then that so is the current status of any possible answer.
If you'd rather avoid revealing your code, but need to understand how to reverse the transformation, you might try to come up with a minimal working example that is succinct and generalized enough that you're comfortable sharing it.
채택된 답변
Walter Roberson
2021년 6월 27일
chr = reshape( chr, 8,[] );
chr will be 8 by something
secret_message3 = chr(:,1:195)
secret_message3 will be 8 by 195 (or an error if chr was not at least 195 columns)
secret_messageF = chr(1:length(secret_message3))
length(X) is defined as:
SZ = size(X);
if any(SZ == 0)
LENGTH = 0
else
LENGTH = max(SZ)
end
so it is 0 if the array is empty in any dimension, and otherwise is the longest dimension no matter which one that is .
With secret_messageF being 8 x 195, the longest dimension is 195, so length(secret_messageF) is 195.
So you are taking chr(1:195) which is indexing a 2D array with a single dimension. That operation is well defined in MATLAB, and means that you will get the first 195 consecutive memory locations out of the array. Arrays are stored "down" first, so A(1,1) then A(2,1) then A(3,1), A(4,1), A(5,1), A(6,1), A(7,1), A(8,1), A(1,2), A(2,2), A(3,2) and so on. chr has 8 rows, so the first 195 entries in memory is the first 24 full columns plus another 3 from the 25th column.
It seems pretty unlikely that that would be what you would want.
추가 답변 (1개)
darren Clipson
2021년 6월 27일
편집: darren Clipson
2021년 7월 6일
댓글 수: 10
Walter Roberson
2021년 7월 3일
I suggest you have a look at https://www.mathworks.com/matlabcentral/answers/466636-caesar-cyphor-encryption-problem?s_tid=srchtitle
참고 항목
카테고리
Help Center 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!