How to split this?

조회 수: 6 (최근 30일)
Younghun Kim
Younghun Kim 2020년 6월 12일
댓글: Younghun Kim 2020년 6월 12일
I have M as a result of another function.
and I have to split M like C automatically...
I want to split it every 16 bits.
M => input split function => output C
M = '01100001011000100110001110000000000000000000000000000000000000000000000000000000'
????
C = {'0110000101100010'; '0110001110000000'; '0000000000000000'; '0000000000000000';'0000000000000000';}
how to make it? I'm stuck on this problem...
can you help me?

채택된 답변

Monika Jaskolka
Monika Jaskolka 2020년 6월 12일
편집: Monika Jaskolka 2020년 6월 12일
C = reshape(M, 16,[])'
C =
5×16 char array
'0110000101100010'
'0110001110000000'
'0000000000000000'
'0000000000000000'
'0000000000000000'
If you need it to end up as a cell array or chars, you can wrap it in cellstr:
C = cellstr(reshape(M, 16,[])')
C =
5×1 cell array
{'0110000101100010'}
{'0110001110000000'}
{'0000000000000000'}
{'0000000000000000'}
{'0000000000000000'}
  댓글 수: 1
Younghun Kim
Younghun Kim 2020년 6월 12일
thank you very much :)

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Image Analyst
Image Analyst 2020년 6월 12일
Did you try a simple for loop:
M = '01100001011000100110001110000000000000000000000000000000000000000000000000000000'
counter = 1;
for index1 = 1 : 16 : length(M)
index2 = min(length(M), index1 + 15);
c{counter} = M(index1 : index2);
counter = counter + 1;
end
celldisp(c)
  댓글 수: 2
Image Analyst
Image Analyst 2020년 6월 12일
Note: This robust, general purpose loop works even if M is not a multiple of 16 characters. Otherwise (if it is known to be a multiple of 16) I'd use reshape().
Younghun Kim
Younghun Kim 2020년 6월 12일
thank you very much :)

댓글을 달려면 로그인하십시오.


David Hill
David Hill 2020년 6월 12일
You could make a matrix
newMatrix=reshape(M(1:floor(length(M)/16)*16),16,[])';
  댓글 수: 1
Younghun Kim
Younghun Kim 2020년 6월 12일
thank you very much :)

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2020년 6월 12일
temp = textscan(M,'%16s');
C = temp{1};
This handles M that are not multiples of 16 characters.
Note, though, that if M contains whitespace, that it will not split by 16 character groups, and would instead treat each "word" as the beginning of a section to be split.
  댓글 수: 1
Younghun Kim
Younghun Kim 2020년 6월 12일
thank you very much :)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by