Matlab function to take matrix as input and return elements in its four corners as output
Ältere Kommentare anzeigen
Hello, good day all, please I'm having difficulties writing a function to take a matrix input, and return the elements in its cour corners as an output.
Here is my code:
function [top_left,top_right,bottom_left,bottom_right] = corners(i,r,c)
A = matrix(i,r,c)
top_left = A(1,1);
top_right = A(1,c);
bottom_left = A(r,1);
bottom_right = A(r,c);
function B = matrix(i,r,c)
ind = i;
row = r;
col = c;
B = randi(ind,row,col);
I keep getting Not enough input arguments.
Error in corners (line 2)
A = matrix(i,r,c)
I'll appreciate your response. Thanks.
5 Kommentare
Naveen Gehlot
am 6 Jun. 2020
function [top_left,top_right,bottom_left,bottom_right] = corners(A)
Acorners=A([1,end],[1,end]);
top_left = Acorners(1,1);
top_right = Acorners(1,2);
bottom_left = Acorners(2,1);
bottom_right = Acorners(2,2);
end
it will works.
Saishyam Balaji
am 11 Jun. 2020
Ya..it's a nice perspective for both vector and matrices
Parth Sanghavi
am 20 Jul. 2020
can you tel me why did we use "Acorners" in the above code
Jonathan Deepak
am 1 Okt. 2020
Bearbeitet: DGM
am 12 Feb. 2023
you can also use the function like this
function [a,b,c,d]= corners(l)
a= l(1,1);
b=l(1,end);
c=l(end,1);
d=l(end,end);
end
Akzeptierte Antwort
Weitere Antworten (5)
mayank ghugretkar
am 3 Jun. 2019
function [top_left,top_right,bottom_left,bottom_right] = corners(M)
top_left = M(1,1);
top_right = M(1,end);
bottom_left = M(end,1);
bottom_right = M(end,end);
end
here's the code to call:
A = randi(100,4,5)
[top_left, top_right, bottom_left, bottom_right] = corners(A)
B = [1; 2]
[top_left, top_right, bottom_left, bottom_right] = corners(B)
I think this will be much simpler approch..
3 Kommentare
Risa Dwi Ratnasari
am 1 Dez. 2019
Hello... Thank you for your explanation. I am glad to see your reply.
btw can you explain to me what the meaning of randi (100, 4, 5)
ilker melik
am 2 Mai 2020
it gives 4x5 matrix. each element of the matrix take values up to 100.
vaibhav jadhav
am 7 Jun. 2020
its a simpler and easy way to do it
I get no errors of any kind when I run your code, e.g.
[top_left,top_right,bottom_left,bottom_right] =corners(10,10,10)
gives
top_left =
4
top_right =
7
bottom_left =
7
bottom_right =
4
The only reason I can think of is you have a another function called matrix() higher in your path which is hiding the one you really want. Try,
>>which -all matrix
4 Kommentare
Matt J
am 6 Feb. 2019
Yes, but the OP has not told us whether matrix() is local to corners().
Jos (10584)
am 7 Feb. 2019
Bearbeitet: Jos (10584)
am 7 Feb. 2019
Strictly speaking, your function corners does not take a matrix input.
I think you asked to do something like this
function out = corners(M)
% M is the input matrix
out(1) = M(1,1)
% etc
Amos Agbetile
am 7 Feb. 2019
I Don't See Any Particular Problem With That Line.
The Problem Is Possibly That You Didn't Send Input Or Enough Input To corners When You Invoked It.
Try Calling Cornenrs From The Command Window With Valid Values, Or When You Run Using the Run Button T The Top Or Using F5, You Can Specify Input At The Run Button Menu
harish kolla
am 16 Jun. 2019
function [top_left,top_right,bottom_left,bottom_right]= corners (A)
top_left= A(1,1)
top_right= A(1,end)
bottom_left=A(end,1)
bottom_right=A(end,end)
end
4 Kommentare
ilker melik
am 2 Mai 2020
for A = randi(10,4,5)
A =
5 6 6 7 2
2 8 6 8 9
5 10 7 5 3
10 6 8 7 4
>> corners(A)
top_left =
5
top_right =
2
bottom_left =
10
bottom_right =
4
ans = %<------ why does MATLAB return "ans" here ?
5
Steven Lord
am 2 Mai 2020
That is the output argument from your call to the corners function, which by the way you've defined it is the contents of the top_left variable inside your function. Note that none of the variables top_left, top_right, bottom_left, or bottom_right will be accessible after this call. They were created inside the function and were destroyed when the function finished execution and its workspace was destroyed.
If you want those variables to be accessible on a future line, call your corners function with between 1 and 4 output arguments, depending on how many of the variables you want to have access to. You probably will also want to end those lines in corners with semicolons so the variables don't get displayed while the function is executing.
[TL, TR, BL, BR] = corners(A);
ilker melik
am 3 Mai 2020
Bearbeitet: ilker melik
am 3 Mai 2020
I get this idea of output argument and accesibility. But I defined also top_right, bottom_left and bottom_right as output argument. But why does particularly top_left pops out as an ans instead of other output arguments.
Stephen23
am 7 Jun. 2020
"But I defined also top_right, bottom_left and bottom_right as output argument."
You defined them in the function, but you did not use any output arguments when calling the function.
How to call functions (with multiple output arguments) is explained in Steven Lords comment above and also in the introductory tutorials:
nor el houda bouhaddoun
am 19 Mai 2020
please can anybody help me because i don't know where the probleme is :
function [top_left, top_right, bottom_left, bottom_right]= corner(A)
top_left= A(1,1);
top_right= A(1,end);
bottom_left= A(end,1);
bottom_right = A(end, end);
end
and it gives me this:
>> A = randi(100,4,5)
??? Undefined function or method 'randi' for input arguments of type 'double'.
and the m-file is in the current directory
2 Kommentare
Stephen23
am 19 Mai 2020
@nor el houda bouhaddoun: please show us the output of this command:
which randi -all
What MATLAB version are you using?
Is this your own computer and MATLAB installation, or that of your school/university/whatever ?
DGM
am 6 Jun. 2024
For sake of curiosity:
randi() was introduced to the base toolbox in R2008b, so for it to be missing in 2020 would be curious.
Kategorien
Mehr zu Track Objects and Estimate Motion finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!