Matrix Determination Issue in getting hidden inputs
1 回表示 (過去 30 日間)
古いコメントを表示
We had given a code ro write an Octave code to find the product of two matrices A and B, element-wise, and then reverse the rows.
Print them, and then find the determinant of the resulting matrix. Below is one of custom inputs which are visible to us, rest does not.
3
3
1 2 3
2 3 4
1 3 5
2 3 4
1 3 5
4 5 6
Sample Output:
Reversed_Matrix = 4 15 30
2 9 20
2 6 12
Determinant = 12
The first and second row denote the dimensions of the first and second square matrices. The next three rows denote the values in the first matrix, and the last three rows denote the values of the next matrix.
We written code as below which is correct, but we do not want it to be hard coded value, it pull it from custom input as mentioned above, please help how to remove value input as hard coded i.e. value which we providing in Reversed_Matrix. This we asking because we have 4 test case. For 1 we can see input, hence as per it we developed code, for rest 3 were not know what will be input hence do not want not any hard code input.
We are getting same output for test 1 but other 3 hidden test cases failled.
function matr()
Reversed_Matrix= flip([1 2 3; 2 3 4 ; 1 3 5].* [2 3 4; 1 3 5; 4 5 6])
Determinant= det(Reversed_Matrix)
endfunction
matr()
採用された回答
Sara Boznik
2020 年 8 月 16 日
function matr()
if [n,n]==size(A) & [m,m]=size(B) & n==m
C=zeros(n,n);
D=zeros(m,m);
C=A;
D=B;
E=C.*D;
Reversed_Matrix=flip(E)
sol=det(Reversed_Matrix)
else fprintf('Determinant does not exists.')
end
endfunction
n=input('n')
m=input('m')
A=input('A');
B=input('B');
matr()
29 件のコメント
Senthilkumar Ramani
2021 年 5 月 8 日
Try this code.
function matr()
# Enter code. Read input from STDIN. Print output to STDOUT.
x = scanf("%d", "C");
y = scanf("%d", "C");
for i=1:x
for j=1:x
A = scanf("%d", "C");
AA(i,j)=A;
end
end
for i=1:y
for j=1:y
B = scanf("%d", "C");
BB(i,j)=B;
end
end
disp("Reversed_Matrix =\n"), disp(flip(AA .* BB));
printf("\nDeterminant = %d", det(flip(AA .* BB))), printf("\n");
endfunction
matr()
その他の回答 (4 件)
Sara Boznik
2020 年 8 月 16 日
In function you write:
function Determinant = matr (A,B)
Reversed_Matrix=flip(A.*B);
Determinant=det(Reversed_Matrix);
end
In script you write:
A=input('Write your first matrix:')
B=input('Write your second matrix:')
determinant=matr(A,B)
Best of luck.
12 件のコメント
Sara Boznik
2020 年 8 月 16 日
You need matrix n*n that you can have determinant.
If you don't have same dimension of matrix determinant not exist. You have to have SQUARE MATRIX.
Sara Boznik
2020 年 8 月 16 日
Script:
% n=input(""); that part is actually no needed
% m=input("");
A=input('A');
B=input('B');
matr(A,B)
Function:
function [Reversed_Matrix, Determinant] = matr(A,B)
[n,n]=size(A);
[m,m]=size(B);
C=zeros(n,n);
D=zeros(m,m);
C=A;
D=B;
E=C.*D;
Reversed_Matrix=flip(E)
Determinant= det(Reversed_Matrix)
end
Also you have attached it.
4 件のコメント
Senthilkumar Ramani
2021 年 5 月 8 日
This code is working perfectly.
function matr()
x = scanf("%d", "C");
y = scanf("%d", "C");
for i=1:x
for j=1:x
A = scanf("%d", "C");
AA(i,j)=A;
end
end
for i=1:y
for j=1:y
B = scanf("%d", "C");
BB(i,j)=B;
end
end
disp("Reversed_Matrix="), disp(flip(AA .* BB));
printf("Determinant=%d", det(flip(AA .* BB))), printf("\n");
endfunction
matr()
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!