MATLAB: How to take 3 elements at a time from an array and turn them into their own column vector

arraysencryptionloops

I am working on a Hill-3 cipher encryption program, and so far I have the code to turn a user inputted string into an array C with each letter assigned a value from 0-25 (I'm not using any other symbols than the english alphabet). What I'm stuck on is how to convert this array C into a series of column vectors such that column vector 1 = [C(1);C(2);C(3)], column vector 2 = [C(4);C(5);C(6)], and so forth. Since the amount of elements in array C will vary, I need to loop this function somehow, and also need to add values to the extra rows of the last column vector if the amount of elements in C is not a multiple of 3. This is what I have so far (ps: I know there is probably a much more efficient way to do what I did with all the different cases, but that's not my issue lol):
prompt = 'Enter message for encryption: ';
msg = input(prompt, 's');
msg = msg(~isspace(msg));
msg = lower(msg);
C = [];
for k = 1:length(msg)
switch msg(k)
case 'a'
C = [C 0];
case 'b'
C = [C 1];
case 'c'
C = [C 2];
case 'd'
C = [C 3];
case 'e'
C = [C 4];
case 'f'
C = [C 5];
case 'g'
C = [C 6];
case 'h'
C = [C 7];
case 'i'
C = [C 8];
case 'j'
C = [C 9];
case 'k'
C = [C 10];
case 'l'
C = [C 11];
case 'm'
C = [C 12];
case 'n'
C = [C 13];
case 'o'
C = [C 14];
case 'p'
C = [C 15];
case 'q'
C = [C 16];
case 'r'
C = [C 17];
case 's'
C = [C 18];
case 't'
C = [C 19];
case 'u'
C = [C 20];
case 'v'
C = [C 21];
case 'w'
C = [C 22];
case 'x'
C = [C 23];
case 'y'
C = [C 24];
case 'z'
C = [C 25];
end
end

Best Answer

msg = 'gfewqaxcvbhgf'; % example
[~,C] = ismember(msg,'a':'z');
C = C - 1;
out = reshape([C(:);zeros(mod(-numel(C),3),1)],3,[]);
Related Question