Using while loops in matrices

I am trying to use while loop to change the diagonal entries of a square matrix rand(10) to 1, and other entries to zero
This code below is changing the whole entries to 1, i am stucked.
m= 1:10
n= 1:10
A = rand(10)
B = size (A)
while m==n
A(m,n) = 1;
if not (m==n)
A(m,n) = 0;
end
break
end
A

回答(1 个)

Let me know if this is what you are looking for. Happy to answer any further questions!
A = rand(10)
A = 10×10
0.1406 0.9234 0.5213 0.0271 0.2317 0.3967 0.3776 0.4847 0.3080 0.0222 0.4363 0.4473 0.2888 0.3199 0.6071 0.3528 0.3311 0.5584 0.4040 0.2190 0.2656 0.6738 0.6987 0.3731 0.8456 0.9905 0.1035 0.4882 0.4701 0.2163 0.0655 0.9590 0.1978 0.6575 0.6074 0.2159 0.2553 0.3445 0.9210 0.6419 0.8856 0.9335 0.8339 0.5396 0.2503 0.0700 0.4051 0.7461 0.3052 0.1634 0.4934 0.4743 0.6264 0.2365 0.6042 0.4961 0.4183 0.7427 0.0465 0.3843 0.2964 0.5497 0.6846 0.4194 0.2852 0.9456 0.7047 0.9608 0.4107 0.6736 0.7785 0.3134 0.1016 0.5672 0.2764 0.7794 0.3809 0.2773 0.2288 0.1847 0.6817 0.5795 0.6303 0.4710 0.6249 0.1779 0.3139 0.5706 0.5860 0.4254 0.6862 0.1720 0.4387 0.9392 0.0916 0.3381 0.4475 0.3649 0.5224 0.7542
for m = 1:10 % loop through rows
for n = 1:10 % loop through columns
if m == n % check if it's diagonal el
A(m,n) = 1;
else
A(m,n) = 0;
end
end
end
A
A = 10×10
1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1

7 个评论

This is using the FOR loop. I want to use the WHILE loop for achive this same result
How about this:
% Initialize matrix A with random values
N = 5;
A = rand(N)
A = 5×5
10 2 6 7 8 6 2 7 9 7 3 6 6 2 4 2 6 6 4 9 1 7 3 1 1
% processing the elements
while N > 0
A(N,:) = 0;
A(N,N) = 1;
N = N-1;
end
A
A = 5×5
1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1
A = rand(10)
A = 10×10
0.5267 0.0485 0.6881 0.0751 0.5088 0.9623 0.4144 0.0369 0.5932 0.1203 0.0743 0.4432 0.1701 0.8212 0.2084 0.4752 0.3201 0.4127 0.1457 0.4379 0.4294 0.8825 0.5937 0.8722 0.1412 0.2457 0.6319 0.1385 0.0125 0.1849 0.1902 0.2588 0.2948 0.1180 0.3554 0.0058 0.1091 0.1462 0.6565 0.7121 0.3901 0.4582 0.6801 0.5591 0.1280 0.3830 0.4597 0.2188 0.6096 0.7307 0.7630 0.4357 0.6976 0.8166 0.7730 0.5614 0.6620 0.1153 0.8800 0.3424 0.9485 0.5221 0.1301 0.3194 0.6058 0.7353 0.1995 0.8348 0.8918 0.8874 0.3259 0.7321 0.2832 0.4322 0.1251 0.5902 0.5059 0.1353 0.5748 0.5123 0.2665 0.2283 0.9713 0.8249 0.1909 0.8411 0.5445 0.3293 0.1103 0.8057 0.1126 0.5050 0.2498 0.4914 0.5326 0.4910 0.3327 0.8345 0.2003 0.1004
m = 1;
while m <= 10 % loop through rows
n = 1;
while n <= 10 % loop through columns
if m == n % check if it's diagonal el
A(m,n) = 1;
else
A(m,n) = 0;
end
n = n + 1;
end
m = m + 1;
end
A
A = 10×10
1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1
Was gonna comment exactly what @Walter Roberson showed. That should solve your problem :)
Thank you for this.. this solves it
The pattern:
if m == n
A(m,n) = 1;
else
A(m,n) = 0;
end
can be abbreviated in general to:
A(m, n) = (m == n);
The whole thing abbreviates to a call to eye() and size()

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

产品

版本

R2022b

提问:

2022-12-17

Community Treasure Hunt

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

Start Hunting!

Translated by