Reverse operation of horzcat, reshape and dec2bin code I have written.

5 views (last 30 days)
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 Comments
darren Clipson
darren Clipson on 27 Jun 2021
Hello dbp,
thanks for you comment.
The 'car crash' is actually in my head trying to process the inverse operation because of lack of knowledge and experience. I can't really apologise for that.
I understand the concatination effect from using horzcat. At this stage, my 1x1560 string becomes 8x195 array.
As for the code, well it could be anything arriving into this section I have posted. I am struggling to understand how to reverse the 'reshape' and 'horzcat' functions. I am not asking to unencrypt fully as there is much more to the code I have written but I already have those sections working. When I intoduce this code here, I have issues but I like what it does.
DGM
DGM on 27 Jun 2021
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.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 27 Jun 2021
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 Comment
darren Clipson
darren Clipson on 28 Jun 2021
Hi, thanks for the reply. I have made some progress actually over the last 24 hours. It does seem I need to spend a lot of time understanding the process more step by step.

Sign in to comment.

More Answers (1)

darren Clipson
darren Clipson on 27 Jun 2021
Edited: darren Clipson on 6 Jul 2021
Ok,
What my code does is imports a text file and uses basic ASCII advancement using a key that is selectable by the user. I have removed some steps in the key application stage but I think you should be able to understand what is happening. At the output from the key advancement stage, there is a single 1 x 1560 string of ASCII characters. It is at this stage the rehape takes place and the concatination
  10 Comments
darren Clipson
darren Clipson on 3 Jul 2021
That’s super helpful! Thanks so much for your time and help Walter. I have progressed quite a lot today with my understanding :)

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!