特定の行列の置き換えに方法ついて
16 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
flower
am 6 Mär. 2023
Kommentiert: flower
am 6 Mär. 2023
Aという10行50列のデータのうち、Bという3行50列に格納されている行番号のデータのみを0に置きかえる方法はありますでしょうか。
Bには、0<10が入っています。
イメージとしては、B = [1;3;5, 2;4;6, 8;7;4,....];
の場合、Aの1列目の1,3,5行が0に、2列目の2,4,6行が0に...
と出来れば思います。
0 Kommentare
Akzeptierte Antwort
Atsushi Ueno
am 6 Mär. 2023
A = rand(10,50); % Aという10行50列のデータ
B = randi(size(A,1),3,50); % Bという3行50列のデータ(0<10)
B(1:3,1:3) = [1,2,8;3,4,7;5,6,4]; % イメージとしては、B = [1;3;5, 2;4;6, 8;7;4,....];
for j = 1:size(B,2)
for k = 1:size(B,1)
A(B(k,j),j) = 0; % Bという3行50列に格納されている行番号のデータ(0<10)のみを0に置きかえる
end
end
A % Aの1列目の1,3,5行が0に、2列目の2,4,6行が0に...
3 Kommentare
Atsushi Ueno
am 6 Mär. 2023
Bearbeitet: Atsushi Ueno
am 6 Mär. 2023
for 文を使ったら負けシリーズ第二弾「for 文を使うな! 2」
A = rand(10,50); % Aという10行50列のデータ
B = randi(size(A,1),3,50); % Bという3行50列のデータ(0<10)
B(1:3,1:3) = [1,2,8;3,4,7;5,6,4]; % イメージとしては、B = [1;3;5, 2;4;6, 8;7;4,....];
B = sub2ind(size(A), B(:), repelem(1:size(A,2),3)'); % Bを添字から線形インデックスに変換する
A(B) = 0 % Aを線形インデックスで参照して0にする
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!