Matrix Determination Issue in getting hidden inputs
5 次查看(过去 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 个评论
Ashish Jindal
2020-8-16
编辑:Ashish Jindal
2020-8-16
We getting below error-
- parse error near line 2 of file /tmp/submission/20200816/17/31/hackerrank-38209e8fdf1cc7164856f342f6789d07/code/Solution.octave
- invalid left hand side of assignment
- >>> if [n,n]==size(A) & [m,m]=size(B) & n==m
- ^
- error: source: error sourcing file '/tmp/submission/20200816/17/31/hackerrank-38209e8fdf1cc7164856f342f6789d07/code/Solution.octave'
In other istance same issue
Error(s), warning(s):
parse error near line 3 of file 611546326/source_rextester.m invalid left hand side of assignment >>> if [n,n]==size(A) & [m,m]=size(B) & n==m ^ error: source: error sourcing file '611546326/source_rextester.m'
Ashish Jindal
2020-8-16
We had corrected code now getting below issue
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()
Error is
- parse error:
- syntax error
- >>> 1 2 3
- ^
- error: called from
- Solution.octave at line 15 column 2
Ashish Jindal
2020-8-16
编辑:Ashish Jindal
2020-8-16
Seems we need to do below thing which will fetch some output which will nto match as what expect but code need to work if below direction, which need further modification
function matr()
n=input("");
m=input("");
A=rand(n);
B=rand(m);
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)
Determinant= det(Reversed_Matrix)
else fprintf('Determinant does not exists.')
end
endfunction
matr()
Here out will be like
Reversed_Matrix =
0.634078 0.398280 0.120195
0.790813 0.083681 0.334776
0.164293 0.095670 0.069930
Determinant = -0.0092764
Somehow we need to input customer input here.
Ashish Jindal
2020-8-16
编辑:Ashish Jindal
2020-8-16
See as told earlier we have 5 test cases, we can see what system will input other we cannot. For which we can see input is as
3
3
1 2 3
2 3 4
1 3 5
2 3 4
1 3 5
4 5 6
Our condition is that from above input we need to prepare A and B matrix. Here condition is 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. If fist row is 3 and second row is 3 then next 3 will be value for A and after whcih next 3 will value for B. Here it is possible then in hidden case ist row shows as 2 , second as 2 then next 2 row will be matrix for A and next 2 will be for B. Simillarly row 1 can be 4 and second can be 4 also. Then next 4 rows will be matrix for A and next 4 will be for B. Like
2
2
1 2
2 3
2 3
1 3
4
4
1 2 3 1
2 3 4 0
1 3 5 8
1 2 3 1
2 3 4 1
1 3 5 3
4 5 6 5
1 2 3 7
We will not able to see this input, so we need to prepare will we run code it automatically prepare matrix A and B as per hidden inputs and we get desire results.
Ashish Jindal
2020-8-16
No.. plz see screen show where we doing testing. There is only 1 place where we write code. It automatically ececute all test cases. We just need to write code and execute it. We cannot provide any input for it.
Sara Boznik
2020-8-16
That is the last idea which I have it:
hiddeninputs=load('dataname.txt')
n=hiddeninputs(1,1)
m=hiddeninputs(2,1)
if n==m
for i=1:n
A=hiddeninputs(n+1+i,i)
B=hiddeninputs(2*n+1+i,i)
end
end
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
Sara Boznik
2020-8-16
Sorry, I tried my best. I still do not understand what you have to do. I taught that the task is that you see the input and you have to get determinant and reverse matrix. But you said that you can see only 1 input, so I do not understand how you will get output without knowing what is input :(
Good luck.
Ashish Jindal
2020-8-17
Please review below code, we think we can try it but some issue in it
function matr()
x = scanf("%d", "C");
y = scanf("%d", "C");
for i=1:5
x(i) = scanf("%d", "C");
end
for j=1:5
y(j) = scanf("%d", "C");
end
for i=1:3
for j=1:2
A = scanf("%d", "C");
end
end
for i=1:3
for j=1:2
B = scanf("%d", "C");
end
end
n=x;
m=y;
C=zeros(n,n);
D=zeros(m,m);
C=A;
D=B;
E=C.*D;
Reversed_Matrix=flip(E)
Determinant= det(Reversed_Matrix)
endfunction
matr()
Ashish Jindal
2020-8-17
New code is
function matr()
x = scanf("%d", "C");
y = scanf("%d", "C");
for i=1:x
for j=1:x
A = scanf("%d", "C");
printf("%d", A);
end
end
for i=1:y
for j=1:y
B = scanf("%d", "C");
printf("%d", B);
end
end
A
B
endfunction
matr()
Here output is as
123234135
234135456
5
6
Hence if we determinate then i is coming as 30, might be some slight issue in code
Sara Boznik
2020-8-17
Did you get A and B as vector?
My Matlab does not know function scanf, what is function for?
Ashish Jindal
2020-8-17
编辑:Ashish Jindal
2020-8-17
Here A value is 123234135
Here B value is 234135456
This via printf
In outside loop
A = 5
B = 6
Ashish Jindal
2020-8-17
scanf is used to scan custom user input. Here how we can convert value of A and B in vecotr?
Sara Boznik
2020-8-17
Outside the loop you just get only the last number. You have to save A like matrix nxn that you can get the right determinant. You need A and B like matrix not vector.
My suggestion is:
function matr()
x = scanf("%d", "C"); %%Is that size of matrix?
y = scanf("%d", "C");
for i=1:x
for j=1:x
A = scanf("%d", "C");
AA=zeros(x,x) %%with zeros you can define the dimension of AA
AA(i,j)=A(i,j) %%this should change the same position elements in AA with values from A
end
end
for i=1:y
for j=1:y
B = scanf("%d", "C");
BB=zeros(y,y)
BB(i,j)=B(i,j)
end
end
endfunction
matr()
Ashish Jindal
2020-8-17
x = scanf("%d", "C"); %%Is that size of matrix?
Here value of x will be just 3. It is not size.
- error: A(_,2): but A has size 1x1
- error: called from
- matr at line 8 column 8
- Solution.octave at line 20 column 1
Ashish Jindal
2020-8-17
编辑:Ashish Jindal
2020-8-17
function matr()
x = scanf("%d", "C");
y = scanf("%d", "C");
for i=1:x
for j=1:x
A = scanf("%d", "C");
[x,x]=size(A);
AA=zeros(x,x)
AA(i,j)=A(i,j)
end
end
for i=1:y
for j=1:y
B = scanf("%d", "C");
[y,y]=size(B);
BB=zeros(y,y)
BB(i,j)=B(i,j)
end
end
endfunction
Can we do it?
Sara Boznik
2020-8-17
Reverse matrix and determinant you get the same like before but as I understand you right first you have to get matrix A and B?
Ok, so x define size if x=3, dimension of matrix is 3x3.
How A is size 1x1 if you defined that x=3?
Maybe you can try only AA(i,j)=A.
Ashish Jindal
2020-8-17
Written as
function matr()
x = scanf("%d", "C");
y = scanf("%d", "C");
for i=1:x
for j=1:x
A = scanf("%d", "C");
AA=zeros(x,x) ;
AA(i,j)=A
end
end
for i=1:y
for j=1:y
B = scanf("%d", "C");
BB=zeros(y,y);
BB(i,j)=B
end
end
endfunction
matr()
Output as
- AA =
- 1 0 0
- 0 0 0
- 0 0 0
- AA =
- 0 2 0
- 0 0 0
- 0 0 0
- AA =
- 0 0 3
- 0 0 0
- 0 0 0
- AA =
- 0 0 0
- 2 0 0
- 0 0 0
- AA =
- 0 0 0
- 0 3 0
- 0 0 0
- AA =
- 0 0 0
- 0 0 4
- 0 0 0
- AA =
- 0 0 0
- 0 0 0
- 1 0 0
- AA =
- 0 0 0
- 0 0 0
- 0 3 0
- AA =
- 0 0 0
- 0 0 0
- 0 0 5
- BB =
- 2 0 0
- 0 0 0
- 0 0 0
- BB =
- 0 3 0
- 0 0 0
- 0 0 0
- BB =
- 0 0 4
- 0 0 0
- 0 0 0
- BB =
- 0 0 0
- 1 0 0
- 0 0 0
- BB =
- 0 0 0
- 0 3 0
- 0 0 0
- BB =
- 0 0 0
- 0 0 5
- 0 0 0
- BB =
- 0 0 0
- 0 0 0
- 4 0 0
- BB =
- 0 0 0
- 0 0 0
- 0 5 0
- BB =
- 0 0 0
- 0 0 0
- 0 0 6
Sara Boznik
2020-8-17
Ok, thats look fine, we just have to remember how to save previous matrix.
Maybe the solution is that you delete zeros and write only:
AA(i,j)=A
Ashish Jindal
2020-8-17
We written as
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
endfunction
matr()
Output as
- AA = 1
- AA =
- 1 2
- AA =
- 1 2 3
- AA =
- 1 2 3
- 2 0 0
- AA =
- 1 2 3
- 2 3 0
- AA =
- 1 2 3
- 2 3 4
- AA =
- 1 2 3
- 2 3 4
- 1 0 0
- AA =
- 1 2 3
- 2 3 4
- 1 3 0
- AA =
- 1 2 3
- 2 3 4
- 1 3 5
- BB = 2
- BB =
- 2 3
- BB =
- 2 3 4
- BB =
- 2 3 4
- 1 0 0
- BB =
- 2 3 4
- 1 3 0
- BB =
- 2 3 4
- 1 3 5
- BB =
- 2 3 4
- 1 3 5
- 4 0 0
- BB =
- 2 3 4
- 1 3 5
- 4 5 0
- BB =
- 2 3 4
- 1 3 5
- 4 5 6
Ashish Jindal
2020-8-17
Thanks a lot issue resolved, final code is
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
E=AA.*BB;
Reversed_Matrix=flip(E)
Determinant= det(Reversed_Matrix)
endfunction
matr()
Sara Boznik
2020-8-17
Sorry that it tooks so long, but I am happy that finally works fine.
Best of luck.
Ashish Jindal
2020-8-17
Not to sorry this was a tricky question need collaboartion from both sidee.
Thanks a lot...
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 个评论
Ashish Jindal
2020-8-16
编辑:Ashish Jindal
2020-8-16
Here we have 4 test cases.... for 1 test case we see input as
3
1 2 3
2 3 4
1 3 5
2 3 4
1 3 5
4 5 6
This mean as 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.
For next 3 we have hidden input which we cannot see. Also we need to write code in, which is also fixed.
function matr()
Write your code here
endfunction
matr()
Ashish Jindal
2020-8-16
Here code is bit tricky might be some pre determinent code hence facing issue how to resolve that
Ashish Jindal
2020-8-16
编辑:Ashish Jindal
2020-8-16
We did following thing for just checking purpose to see what is in hidden data -
function matr()
x=input("");
y=input("");
printf("%d\n", x);
printf("%d\n", y);
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()
We found for 0 and 1 cases it is
3
3
For 2 and 3 it is
2
2
For 4 it is
4
4
So, here we have 3*3, 2*2 and 4*4. We seems need to prepare matrix like it. So basically we need to know what we need to give in plce of ? in below code we think so.
function matr()
x=input("");
y=input("");
A= ? ;
B= ? ;
Reversed_Matrix= flip(A.* B)
Determinant= det(Reversed_Matrix)
endfunction
matr()
Sara Boznik
2020-8-16
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
2020-8-16
We are almots near but still someting mssing, when we written code as
function matr()
x=input("");
y=input("");
A=zeros(x,x);
B=zeros(y,y);
Reversed_Matrix= flip(A.* B)
Determinant= det(Reversed_Matrix)
endfunction
matr()
We getting output as -
- Reversed_Matrix =
- 0 0 0
- 0 0 0
- 0 0 0
- Determinant = 0
It should be as
- Reversed_Matrix =
- 4 15 30
- 2 9 20
- 2 6 12
- Determinant = 12
Sara Boznik
2020-8-16
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
2020-8-16
We written code as
function matr()
n=input("");
m=input("");
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)
Determinant= det(Reversed_Matrix)
endfunction
matr()
getting error as
Compiler Message
Runtime Error
Error (stderr)
- parse error:
- syntax error
- >>> 1 2 3
- ^
- error: called from
- matr at line 4 column 2
- Solution.octave at line 17 column 1
Sara Boznik
2020-8-16
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
2020-8-16
We written as
n=input("");
m=input("");
A=input('A');
B=input('B');
function matr()
[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)
endfunction
matr()
Same issue
Ashish Jindal
2020-8-16
编辑:Ashish Jindal
2020-8-16
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
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 个评论
Ashish Jindal
2020-8-16
Here we have lot of constant value like below thing
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".
Ashish Jindal
2020-8-16
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.
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()
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Java Client Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)