Filter löschen
Filter löschen

cell配列内のst​ring配列を文字数​で分割して、新たなc​ell配列を作る方法​を教えてください。

12 Ansichten (letzte 30 Tage)
K_S_
K_S_ am 21 Jul. 2022
Kommentiert: Akira Agata am 26 Jul. 2022
下記のような10×1のcell配列 cがあります。
cの各要素は16文字で、これを8文字ずつ分割し、
10×2のcell配列 dを作る方法を教えていただきたいです。
よろしくお願いいたします。
a = randn(1,10);
T = numerictype(true,64,60);
F = fimath('OverflowMode', 'saturate',...
'RoundMode', 'round',...
'SumMode', 'FullPrecision',...
'ProductMode', 'FullPrecision',...
'MaxProductWordLength', 256,...
'MaxSumWordLength', 256);
a_fi = fi(a,T,F);
b_fi = hex(a_fi);
c = strsplit(b_fi)'
c = 10×1 cell array
{'0c23f10982cb9580'} {'1acea553c2c99c00'} {'fd1aad0f9ea8e3e0'} {'d0f62114ea683600'} {'ee063c5b90a0a600'} {'e9bcdc9fcc6c0300'} {'ea85ed0353295b00'} {'013a14feabbb7600'} {'0f41bd11cb0c1900'} {'0bc75e880803b780'}

Akzeptierte Antwort

Hernia Baby
Hernia Baby am 21 Jul. 2022
あまりスマートではないと思いますが、cellfunでそれぞれの文字分だけ抽出して最後に結合します。
a = randn(1,10);
T = numerictype(true,64,60);
F = fimath('OverflowMode', 'saturate',...
'RoundMode', 'round',...
'SumMode', 'FullPrecision',...
'ProductMode', 'FullPrecision',...
'MaxProductWordLength', 256,...
'MaxSumWordLength', 256);
a_fi = fi(a,T,F);
b_fi = hex(a_fi);
c = strsplit(b_fi)'
c = 10×1 cell array
{'0c2960861097ff00'} {'2a23874e5a036600'} {'10fc12e56bf04f00'} {'e9f52ae48f30d600'} {'1cb8fac15bd16100'} {'ee6ebe00e0e63f00'} {'f5694d83b4dc7e80'} {'f48c27276158e580'} {'f21b9ea596fe5f80'} {'de10328d26bcf000'}
% 1~8文字だけ抜き出す
c1 = cellfun(@(x) x(1:8),c,'UniformOutput',false)
c1 = 10×1 cell array
{'0c296086'} {'2a23874e'} {'10fc12e5'} {'e9f52ae4'} {'1cb8fac1'} {'ee6ebe00'} {'f5694d83'} {'f48c2727'} {'f21b9ea5'} {'de10328d'}
% 9~最後まで抜き出す
c2 = cellfun(@(x) x(8:end),c,'UniformOutput',false)
c2 = 10×1 cell array
{'61097ff00'} {'e5a036600'} {'56bf04f00'} {'48f30d600'} {'15bd16100'} {'0e0e63f00'} {'3b4dc7e80'} {'76158e580'} {'596fe5f80'} {'d26bcf000'}
% 2つのcellを結合
c3 = [c1, c2]
c3 = 10×2 cell array
{'0c296086'} {'61097ff00'} {'2a23874e'} {'e5a036600'} {'10fc12e5'} {'56bf04f00'} {'e9f52ae4'} {'48f30d600'} {'1cb8fac1'} {'15bd16100'} {'ee6ebe00'} {'0e0e63f00'} {'f5694d83'} {'3b4dc7e80'} {'f48c2727'} {'76158e580'} {'f21b9ea5'} {'596fe5f80'} {'de10328d'} {'d26bcf000'}
  3 Kommentare
Atsushi Ueno
Atsushi Ueno am 21 Jul. 2022
Bearbeitet: Atsushi Ueno am 21 Jul. 2022
細かいですが誤記を指摘します。誤記は回答の方だけで、上記コメントの方は正しくなっています。
c2 = cellfun(@(x) x(8:end),c,'UniformOutput',false) % 9~最後まで抜き出す【誤】
c2 = cellfun(@(x) x(9:end),c,'UniformOutput',false) % 9~最後まで抜き出す【正】
あとどうしても「もっと少ない手で出来ないか」とcody脳が疼きます。strsplit 関数は文字ベクトル や string スカラーしか受け付けませんが、文字ベクトルの cell 配列を受け付ける文字列処理関数も多いです。
c = {'0c23f10982cb9580';'1acea553c2c99c00'}; % 2行コピーしました
d = regexp(c,'.{8}','match'); % 任意の8文字にマッチした文字列を出力する
e = vertcat(d{:}) % ネストした cell 配列を1段階ほどく
e = 2×2 cell array
{'0c23f109'} {'82cb9580'} {'1acea553'} {'c2c99c00'}
Akira Agata
Akira Agata am 26 Jul. 2022
この問題、私もcody脳が疼きました。
以下のような方法ではどうでしょう?
c = {'0c23f10982cb9580';'1acea553c2c99c00'}; % 2行コピーしました
c2 = regexprep(c,'(^\w{8})(\w{8}$)','$1,$2'); % 最初の8文字と最後の8文字の間にカンマを入れる
e = split(c2,',') % カンマでスプリット
e = 2×2 cell array
{'0c23f109'} {'82cb9580'} {'1acea553'} {'c2c99c00'}

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Logical finden Sie in Help Center und File Exchange

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!