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

32 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
Atsushi Ueno
Atsushi Ueno on 6 Mar 2023
Edited: Atsushi Ueno on 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にする
A = 10×50
0 0.1357 0.8594 0.0817 0.9012 0.9353 0.5676 0.0249 0.8765 0.9944 0 0 0.9033 0.0198 0.4167 0.7282 0.0071 0 0.1230 0 0 0 0.6772 0.0765 0.2088 0.2291 0.7593 0.1517 0.3637 0 0.7943 0 0.8825 0.7574 0.1642 0 0.0121 0 0.8586 0.7842 0 0.2693 0.6650 0.6643 0 0 0.6630 0.0152 0.9455 0.1156 0.9668 0.7847 0.1166 0 0.8238 0.0791 0.1323 0.5051 0.7100 0 0 0.1594 0.3565 0.0363 0.5042 0.2520 0 0 0 0.5180 0.7266 0.8572 0.1699 0.9795 0.8283 0.2424 0.3086 0 0.7127 0 0.8867 0.2969 0.5249 0.6126 0.2115 0.8572 0.5446 0.4756 0 0.7542 0.3852 0 0 0.0422 0 0.9528 0.6489 0.3605 0.8136 0.1312 0.3351 0.1636 0.0892 0.2708 0 0.6772 0 0.8242 0 0.5022 0.8300 0.1373 0 0 0 0 0.2354 0.2821 0.5271 0.5323 0 0.4808 0.0168 0.2407 0.5110 0.2110 0.5067 0.7097 0 0.5665 0.6760 0.4823 0 0 0.8300 0.8586 0.5059 0.2325 0.9588 0.9664 0.8051 0 0.4566 0 0 0 0.2838 0.5499 0.2581 0.5155 0.1718 0 0.6915 0 0.2333 0.4086 0.8220 0.0670 0.3443 0 0.2418 0.4943 0 0.6556 0 0.5261 0.1519 0.5556 0.8818 0.3320 0 0.8688 0.5160 0.8026 0.9965 0.4707 0 0.4135 0.0141 0.9135 0.6842 0.4798 0 0.7064 0 0 0.4340 0.8516 0.2051 0.1118 0.3980 0 0.1107 0 0.6546 0.0834 0.6001 0.6534 0 0 0.4385 0 0.1284 0.1555 0.9127 0.8374 0.9816 0.1734 0 0.2541 0.2558 0.5228 0 0.9257 0 0 0 0.9809 0.3137 0 0.5758 0.1306 0.3039 0.5355 0.1078 0.1498 0 0 0.4261 0.7349 0.5894 0.6804 0.1740 0.6219 0.9862 0.7325 0 0.5245 0.7625 0.0855 0.0919 0.6812 0.6527 0 0.7233 0.6858 0.1016 0 0.4064 0.6110 0 0 0.4780 0.6361 0.6517 0 0.7445 0.6611 0 0.6320 0.6082 0.4250 0 0.4658 0.8303 0 0.8014 0 0 0 0.0750 0.4096 0.6157 0.1658 0.4854 0.6870 0.3486 0.1738 0 0 0.6959 0.4733 0 0 0.0842 0.7070 0.9346 0.5912 0.6954 0.7525 0 0.1295 0.9545 0.4123 0 0.7158 0.8816 0 0.9037 0.7394
flower
flower on 6 Mar 2023
やりたいことが実現できました。バリエーション豊富でどれも思いつかないものでした...すごいです。
ありがとうございました。

Sign in to comment.

More Answers (0)

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!