Reverse operation of horzcat, reshape and dec2bin code I have written.
1 次查看(过去 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 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!