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
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!