Reputation: 105
New to Matlab and am trying to convert a binary message to characters in Matlab. I was able to translate the binary message to a single column in decimal (without the bin2dec function, as I was instructed not to), with each number corresponding to a character in a string array.
I am having trouble getting the character string array to show its values at the values for the indexes of another array. It works when I reference individual indexes of an array (e.g. message = char_array(decimal(1)) returns a value of k), but not when I try to reference the whole column or the whole array of decimal (e.g. message = char_array(decimal(:,:)) and message = char_array(decimal(:,1)) do not work and give the same error). I know this can accomplished with for loops, but we have been instructed not to use those yet.
Is there some other trick to getting the entire message to display at once? This needs to work in such a way that it could be used for thousands of letters at once with one simple line of code.
The code so far:
clear;clc
% Array of characters (first value is a space, last is ! which denotes the end of the message)
char_array = ' abcdefghijklmnopqrstuvwxyz!';
% The secret message in binary
secret = [0 1 1 0 0;
0 0 0 0 1;
1 0 0 0 0;
0 1 0 0 0;
1 0 0 1 0;
0 1 1 1 1;
0 0 0 0 1;
0 1 0 0 1;
0 0 1 1 1;
0 0 0 0 0;
1 0 0 1 0;
0 1 1 1 1;
0 0 0 1 1;
0 1 0 1 1;
1 0 0 1 1;
1 1 0 1 1];
% Assigns a decimal value to each column to help convert binary values to decimal
conv = [16 8 4 2 1];
% Converts binary numbers in each column of secret message to a decimal value
convert = [secret(:,1)*conv(1), secret(:,2)*conv(2), secret(:,3)*conv(3), secret(:,4)*conv(4), secret(:,5)*conv(5)];
% Converts unadded decimal values into columns to prepare for addition
binary = convert';
% Adds numbers in each column, then transposes it to a single column of decimal values representing values of each row of original message
decimal = sum(binary)';
% Should display character from character array for each index value of "decimal", but returns "Subscript indices must either be real positive integers or logicals." error each time. Works for individual entries such as decimal(1), though.
message = char_array(decimal(:,:))
Upvotes: 1
Views: 179
Reputation: 221514
Fast matrix multiplication
based technique for a generic compact one-liner solution -
message = char_array(secret*(2.^(size(secret,2)-1:-1:0)')+1)
Upvotes: 2
Reputation: 105
My code was fine. The problem was that one of the values from decimal was zero, and you cannot reference an array value of zero! :)
If you add 1 to each value of decimal to compensate for the space at the beginning of the character array, it yields the message correctly.
Towards the bottom of the code:
% Adds numbers in each column, then transposes it to a single column of decimal values representing values of each row of original message
decimal = sum(binary)';
% Adds 1 to each value of "decimal"
decimal2 = decimal+1;
message = char_array(decimal2(:,:))
Which decodes the message properly. (And yes, Laphroaig does indeed rock!)
Upvotes: 2