特定の行列の置き換えに方法ついて
11 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
Atsushi Ueno
le 6 Mar 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 commentaires
Atsushi Ueno
le 6 Mar 2023
Modifié(e) : Atsushi Ueno
le 6 Mar 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にする
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Resizing and Reshaping Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!