Matrix Determination Issue in getting hidden inputs
Mostrar comentarios más antiguos
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()
1 comentario
Respuesta aceptada
Más respuestas (4)
function Determinant = matr(A,B)
Reversed_Matrix= flip(A.* B) ;
Determinant= det(Reversed_Matrix) ;
end
Save the above function. Now define the matrices A and B call the function
val = matr(A,B) ;
Sara Boznik
el 16 de Ag. de 2020
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 comentarios
Ashish Jindal
el 16 de Ag. de 2020
Editada: Ashish Jindal
el 16 de Ag. de 2020
Ashish Jindal
el 16 de Ag. de 2020
Ashish Jindal
el 16 de Ag. de 2020
Ashish Jindal
el 16 de Ag. de 2020
Editada: Ashish Jindal
el 16 de Ag. de 2020
Sara Boznik
el 16 de Ag. de 2020
Hi! My english is quite bad but if I understand you right you have cases with different dimension of matrix? If you just have different dimension is this not important for matlab code.
Or maybe do you need this:
function 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
Ashish Jindal
el 16 de Ag. de 2020
Sara Boznik
el 16 de Ag. de 2020
A=input('A')
B=input('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)
sol=det(Reversed_Matrix)
Attached you have a screenshot of my output, so I think it works fine.
Best of luck.
Ashish Jindal
el 16 de Ag. de 2020
Sara Boznik
el 16 de Ag. de 2020
n=input("");
m=input("");
A=input('A');
B=input('B');
This part should be written in script not in function, than also in scripr you call the function.
And for end of function you should write only end, not endfunction.
Ashish Jindal
el 16 de Ag. de 2020
Ashish Jindal
el 16 de Ag. de 2020
Editada: Ashish Jindal
el 16 de Ag. de 2020
Sara Boznik
el 16 de Ag. de 2020
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
el 16 de Ag. de 2020
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 comentarios
Ashish Jindal
el 16 de Ag. de 2020
KSSV
el 16 de Ag. de 2020
You straight away copy your matrix as you did in the first test case.
Sara Boznik
el 16 de Ag. de 2020
Is that something for school?
Than I will write only the function.
Ashish Jindal
el 16 de Ag. de 2020
Senthilkumar Ramani
el 8 de Mayo de 2021
0 votos
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()
Categorías
Más información sobre Code Generation en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
