How to split each element in a cell array into four equal parts?
Mostrar comentarios más antiguos
Hello!
In my program, I ask the user to input a string. I then convert each character of that string into 8 bit numbers, which I store individually as elements in a cell array.
I would then like to split each of those elements into 4 equal parts, meaning that each 8 bit number is now split into 4 parts containing 2 bits each.
But I don't know how to do this and loop through each one.
Here's my code so far (the loop doesn't work):
% Ask user to input message
userMessage = input('Please input your message here: ');
% Take individual character and convert to 8 bit binary number
% Do this for each character in the userMessage
userMsgLength = strlength(userMessage);
binaryUserMessage = dec2bin(double(userMessage), 8);
% Isolate each 8 bit number for each character
splitBinaryUserMessage = cellstr(reshape(binaryUserMessage,[],8));
disp(splitBinaryUserMessage);
% Split each 8 bit number into four parts, two bits in each part
for i = 1:length(splitBinaryUserMessage)
part1{i} = splitBinaryUserMessage(1:2);
part2{i} = splitBinaryUserMessage(3:4);
part3{i} = splitBinaryUserMessage(5:6);
part4{i} = splitBinaryUserMessage(7:8);
disp(part1);
disp(part2);
disp(part3);
disp(part4);
end
Please can someone help? Thank you!
2 comentarios
Matt J
el 11 de Abr. de 2022
I recommend providing a small example of input and its desired output.
Eleanor Stevens
el 11 de Abr. de 2022
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!