How can i cut the element in matrix?

1 回表示 (過去 30 日間)
Won seo Ji
Won seo Ji 2016 年 11 月 22 日
回答済み: Walter Roberson 2016 年 11 月 29 日
I want to cut the element in matrix. if i have a matrix like [ 110000111 ] (1x1) i want to make this [ 11 ; 0000 ; 111 ] (3x1) is this possible? If you can only cut it to a certain size, it's okay. like [110000111] to [110 ; 000 ; 111] that's okay too. thks :-)
  1 件のコメント
Walter Roberson
Walter Roberson 2016 年 11 月 22 日
What is the data type of your input matrix? What is the expected data type of the output matrix?

サインインしてコメントする。

回答 (3 件)

Hildo
Hildo 2016 年 11 月 29 日
Change this number to string, so you will can cut.

bio lim
bio lim 2016 年 11 月 29 日
a=[110000111];
b=str2double(regexp(num2str(a),'\d','match'))
count = 1;
for i = 1:3
result(i) = str2num(strcat(num2str(b(count)),num2str(b(count+1)),num2str(b(count+2))));
count = count + 3;
end
result = result'
result =
110
0
111
Since result is double it is not displayed as 000, but 0.
If you keep it as a string without the str2num, then, something like:
result{i} = strcat(num2str(b(count)),num2str(b(count+1)),num2str(b(count+2)));
result =
'110'
'000'
'111'

Walter Roberson
Walter Roberson 2016 年 11 月 29 日
In the special case where you are working with a string
S = '110000111';
result = regexp(S, '(.)(\1*)(?!\1)', 'match');

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by