Matrix Determination Issue in getting hidden inputs

5 visualizzazioni (ultimi 30 giorni)
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()

Risposta accettata

Sara Boznik
Sara Boznik il 16 Ago 2020
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 Commenti
Ashish Jindal
Ashish Jindal il 17 Ago 2020
Not to sorry this was a tricky question need collaboartion from both sidee.
Thanks a lot...
Senthilkumar Ramani
Senthilkumar Ramani il 8 Mag 2021
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()

Accedi per commentare.

Più risposte (4)

KSSV
KSSV il 16 Ago 2020
Modificato: KSSV il 16 Ago 2020
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
Sara Boznik il 16 Ago 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 Commenti
Ashish Jindal
Ashish Jindal il 16 Ago 2020
Modificato: Ashish Jindal il 16 Ago 2020
Our input is like dis
3
3
1 2 3
2 3 4
1 3 5
2 3 4
1 3 5
4 5 6
So, it easily take value for m and n as 3 respectively. But issue is that we think we need to covert
1 2 3
2 3 4
1 3 5
to A matrix, this can be as
1 2 3
2 3 4
If n = 2
Simillarly if m = 3 we need to convert below into B matrix -
2 3 4
1 3 5
4 5 6
if M = 2 then
2 3 4
1 3 5
Sara Boznik
Sara Boznik il 16 Ago 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.

Accedi per commentare.


Sara Boznik
Sara Boznik il 16 Ago 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 Commenti
Sara Boznik
Sara Boznik il 16 Ago 2020
Is that something for school?
Than I will write only the function.
Ashish Jindal
Ashish Jindal il 16 Ago 2020
See we have 5 test cases as we mentioned above... we can see matrix input for 1 case only rest is hidden. Please see our screen shots.
Our task is to write code in below
function matr()
Write your code here
endfunction
matr()
We cannot change function, what ever we need to do need to write in that function only i.e. in "Write your code here".
Hidden test case automatically runs.
We had another test case where 1 test case was visble rest not. We passed it.
function fibo(n)
fib=zeros(1,n);
fib(1)=1;
fib(2)=1;
k=3;
while k <= n
fib(k)=fib(k-2)+fib(k-1);
k=k+1;
endwhile
if (n>1)
fprintf("Fibonacci series: ");
fprintf('%g ',fib);
endif
endfunction
a = input("");
fibo(a)
Here is simillar 1 case input we can see rest we cannot. Inital code for this was as
function fibo(n)
"Write your code here"
endfunction
a = input("");
fibo(a)
We written code in "Write your code here", Which work perfectly. Simillar for this case also we need to write code in same place without touching funtion part.

Accedi per commentare.


Senthilkumar Ramani
Senthilkumar Ramani il 8 Mag 2021
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()

Community Treasure Hunt

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

Start Hunting!

Translated by