The test suite generates a random M x N matrix with a random

The test suite generates a random M x N matrix with a random number of rows and columns that is assigned to the variable A . The element values of the matrix are random double precision numbers that fall in the range from -10 to 10. The matrix has between 5 and 10 rows and between 5 and 10 columns. Assuming A is defined in the workspace, write a script with commands to perform the operations described below and assign the results to the indicated variable names.

Replace all elements in A that have a value less than 0 with the number 999 and assign the resulting matrix to B.

Replace all positive elements in A with their square root and assign the resulting matrix to C.

Find all elements in A with values between -2 and 2, sort the values in a column vector from lowest to highest, and assign to the variable D.

please someone give me a few tips on how to do this

Solution

MATLab code:

clc
clear all
rmin=5;rmax=10;
cmin=5;cmax=10;
M=round(rmin+(rmax-rmin)*rand);
N=round(cmin+(cmax-cmin)*rand);
vmin=-10;vmax=10;
A=vmin+(vmax-vmin)*rand(M,N);
[x,y]=find(A<0);
B=A;
for i=1:length(x)
B(x(i),y(i))=999;
end
[x,y]=find(A<0);
C=A;
for i=1:length(x)
C(x(i),y(i))=sqrt(A(x(i),y(i)));
end
[x,y]=find(A>-2&A<2);
for i=1:length(x)
Val(i)=A(x(i),y(i));
end
D=sort(Val);
A
B
C
D
RESULT:

A =

-4.9042 2.0434 -3.5506 -3.1775 -6.2268
-5.5192 -2.2646 5.6948 2.1478 -4.2500
3.3567 8.3198 -0.5729 -6.1651 -8.1777
6.8878 -9.9770 -9.2847 4.7685 1.5242
-3.1108 -0.7510 -6.4825 -5.1430 3.6673
5.6104 -1.5130 4.4352 8.3485 0.9319
3.5066 -0.7817 -0.5303 -4.6188 -1.4854
-9.8657 5.4032 -6.9456 5.3100 2.8889


B =

999.0000 2.0434 999.0000 999.0000 999.0000
999.0000 999.0000 5.6948 2.1478 999.0000
3.3567 8.3198 999.0000 999.0000 999.0000
6.8878 999.0000 999.0000 4.7685 1.5242
999.0000 999.0000 999.0000 999.0000 3.6673
5.6104 999.0000 4.4352 8.3485 0.9319
3.5066 999.0000 999.0000 999.0000 999.0000
999.0000 5.4032 999.0000 5.3100 2.8889


C =

Columns 1 through 4

0 + 2.2145i 2.0434 0 + 1.8843i 0 + 1.7826i
0 + 2.3493i 0 + 1.5049i 5.6948 2.1478
3.3567 8.3198 0 + 0.7569i 0 + 2.4830i
6.8878 0 + 3.1586i 0 + 3.0471i 4.7685
0 + 1.7637i 0 + 0.8666i 0 + 2.5461i 0 + 2.2678i
5.6104 0 + 1.2300i 4.4352 8.3485
3.5066 0 + 0.8841i 0 + 0.7282i 0 + 2.1491i
0 + 3.1410i 5.4032 0 + 2.6354i 5.3100

Column 5

0 + 2.4953i
0 + 2.0616i
0 + 2.8597i
1.5242
3.6673
0.9319
0 + 1.2188i
2.8889


D =

-1.5130 -1.4854 -0.7817 -0.7510 -0.5729 -0.5303 0.9319 1.5242

The test suite generates a random M x N matrix with a random number of rows and columns that is assigned to the variable A . The element values of the matrix ar
The test suite generates a random M x N matrix with a random number of rows and columns that is assigned to the variable A . The element values of the matrix ar

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site