特定の行列の置き換えに方法ついて

6 views (last 30 days)
flower
flower on 6 Mar 2023
Commented: flower on 6 Mar 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に...
と出来れば思います。

Accepted Answer

Atsushi Ueno
Atsushi Ueno on 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に...
A = 10×50
0 0.7718 0.7895 0.1420 0 0.5693 0.4373 0.4954 0 0 0.7592 0.8207 0.3226 0.3680 0.1719 0.5081 0.7613 0 0 0.4406 0.8574 0.9051 0 0.0606 0.3038 0.6038 0.7309 0.4929 0.1167 0.1499 0.0597 0 0.8033 0 0.2491 0.2651 0.1170 0.2995 0.6122 0.3208 0.9350 0 0.0262 0.5147 0.1819 0.9510 0.2624 0.9878 0 0.7489 0.9119 0.9811 0.4622 0.6916 0.8696 0.6368 0 0.2488 0.6778 0.3306 0 0.0289 0.3459 0.4727 0.9457 0.3140 0 0.1217 0.7499 0.5453 0 0 0.9879 0.5238 0.2765 0.5372 0 0 0.2808 0.1379 0 0 0.6291 0 0.5940 0 0.8516 0.0977 0 0.7673 0.8414 0 0 0.6882 0 0.2365 0.6656 0.9974 0.6479 0.7240 0.1648 0 0 0 0 0.2182 0 0.5141 0.6374 0.4489 0.7500 0 0.1799 0.4598 0 0.5162 0.1979 0 0.5856 0 0 0.5431 0.8756 0 0.6322 0 0 0.4227 0.3913 0.7080 0.7437 0.0055 0.3072 0.1676 0.1763 0.6099 0.9582 0.4855 0.2154 0 0.7996 0 0 0.0206 0.4523 0.5729 0.5933 0.2590 0.3823 0.4316 0.3775 0 0.0405 0 0.0574 0.0673 0.6523 0 0.3517 0 0.4356 0.2352 0 0.9575 0.0908 0.0005 0.0950 0.0451 0.9584 0.3804 0.8418 0.5794 0.5128 0.2612 0.5995 0.5951 0.2944 0.4499 0.2436 0 0.0717 0.6202 0 0.9999 0.4440 0.7030 0.0393 0.7488 0.3140 0.5701 0.6889 0.8160 0.5022 0.1222 0.8667 0.5064 0.3532 0 0.9160 0.6394 0 0.8477 0.9690 0.3574 0.6484 0.7077 0 0.9140 0 0.1547 0.5234 0.5710 0 0.7823 0.0430 0 0.0299 0.5471 0 0 0.0358 0.8269 0.1276 0.4183 0.9646 0 0 0.8720 0.2193 0 0.6064 0.3184 0.4654 0 0 0 0.0294 0.4956 0.0299 0.4631 0.6826 0.9556 0.5362 0.8360 0.6736 0.4998 0.9703 0 0.8535 0.6667 0.7108 0.4505 0.7293 0 0 0.5153 0.0007 0.5709 0 0.9282 0.6700 0.4001 0.6387 0.1857 0.4951 0.7932 0 0 0 0.8176 0.9923 0.4814 0.7679 0.6291 0 0.3932 0 0 0 0.7987 0 0.8770 0 0.2043 0 0 0.2565 0.5743 0.0042 0.5482 0 0.7701 0 0.5745 0.0743 0.8791 0.8381 0 0.4316 0.5902
  3 Comments
flower
flower on 6 Mar 2023
やりたいことが実現できました。バリエーション豊富でどれも思いつかないものでした...すごいです。
ありがとうございました。

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!