1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

cellとcellの結合

1
Last updated at Posted at 2026-06-24

cell1とcell2がある。
こちらはどちらも同じ、
項目A|項目B|値
という列である。これを
項目A|項目B|値1|値2という新たなcellにしたい
行は20000行ほどある
cel1とcell2の並びは同じではない。

■パターン1

  • cell1 の行だけを出力する
  • cell2 にしかない項目は無視する
  • 行順は cell1 の順を維持する
  • 項目A+項目Bは各cell内で一意
  • 20000行規模でループを使わない
% cell1 のキー [項目A, 項目B] が cell2 の何行目か検索
[isFound, loc] = ismember( ...
    cell1(:, 1:2), ...
    cell2(:, 1:2), ...
    'rows');

% 出力を事前確保
resultCell = cell(size(cell1, 1), 4);

% 項目A、項目B、値1をコピー
resultCell(:, 1:3) = cell1;

% 一致した行だけ値2を設定
% cell2にない項目は4列目が [] のままになる
resultCell(isFound, 4) = cell2(loc(isFound), 3);

■パターン2

  • % cell1
  • % 1列目:項目A
  • % 2列目:項目B
  • % 3列目:値1
  • % 4列目:値2(空)
  • % cell2
  • % 1列目:項目A
  • % 2列目:項目B
  • % 3列目:値1(使用しない)
  • % 4列目:値2
% 対応する行を検索
[tf, loc] = ismember(cell1(:,1:2), cell2(:,1:2), 'rows');

% 値2だけコピー
cell1(tf,4) = cell2(loc(tf),4);
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?