how to make a row all zeros in a matrix based on the two random number generated???

조회 수: 1 (최근 30일)
I have 6 matrices namely A,B,C D,E,F and all matrix are of 6*3 order. A is node 1, B -node 2,C-node 3,....F-node6.
Now i have selected two random numbers from 1 to 6. Lets say the outcome is 2 and 6.(it should change for every run)
Now i have to consider node 2 and node 6 i.e B matrix and F matrix. and have to make row 2 of B matrix all zeros and row 6 of F matrix all zeros.
This making of matrix row zeros need to be changed for every run?
How to do this?? please anyone help me in giving some hints.
I

채택된 답변

Chunru
Chunru 2021년 7월 27일
편집: Chunru 2021년 7월 27일
You can arrange A..F into a 3D matrix.
X = randn(6,3, 6);
idx = randperm(6, 2)
idx = 1×2
3 2
for i=1:length(idx)
X(idx(i), :, idx(i)) = 0;
end
X
X =
X(:,:,1) = 0.9829 -0.1786 1.7468 -0.6220 1.4833 0.5872 -0.2820 -0.3638 0.4954 -0.2160 -2.0395 0.6518 0.1525 -0.3734 0.3612 0.7840 -1.6324 -1.8073 X(:,:,2) = -0.5503 -0.5640 0.0966 0 0 0 0.6500 0.4219 0.9918 0.1616 -1.2136 0.7539 -0.3933 0.7912 0.2208 -0.8855 -1.1329 0.4307 X(:,:,3) = 1.2550 0.9819 -0.4042 1.9447 -0.6273 1.8801 0 0 0 -0.0131 -1.8323 1.9285 0.4190 0.2447 0.7425 1.7066 0.0884 -1.0616 X(:,:,4) = 0.1916 -0.7261 0.2208 -0.0329 0.1441 -3.6266 0.3964 0.9971 -1.4898 -1.4636 -1.0939 -0.9044 0.0458 0.0027 0.4354 1.5481 0.5562 -0.7116 X(:,:,5) = 0.4583 1.4250 -0.1841 1.5220 0.0359 -0.6834 -1.9340 0.0564 -1.3511 0.9395 -0.9484 -0.6058 -0.5138 -0.6197 -0.2760 -0.7884 -0.7876 1.5975 X(:,:,6) = -1.2872 -0.3733 -0.0480 0.3810 -1.6373 -0.4153 -1.9955 -0.7583 0.0861 -0.7313 -0.4219 0.7949 0.1781 -0.7183 0.9187 -0.2393 0.0841 -0.8581
  댓글 수: 1
chan
chan 2021년 7월 28일
Thank you. I need this code only
for i=1:length(idx)
X(idx(i), :, idx(i)) = 0;
end
It helps me in getting my desire result.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by