How to split each element in a cell array into four equal parts?
이전 댓글 표시
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
Matt J
2022년 4월 11일
I recommend providing a small example of input and its desired output.
Eleanor Stevens
2022년 4월 11일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!