Matlab function to take matrix as input and return elements in its four corners as output

339 views (last 30 days)
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.
Jonathan Deepak
Jonathan Deepak on 1 Oct 2020
Edited: DGM on 12 Feb 2023
you can also use the function like this
function [a,b,c,d]= corners(l)
a= l(1,1);

Sign in to comment.

Accepted Answer

Matt J
Matt J on 7 Feb 2019
Edited: Matt J on 7 Feb 2019
Maybe this is what you really want:
function Acorners = corners(A)
Matt J
Matt J on 19 Sep 2020
It gets the corners. Did you try it?
>> A=nan(5); A(:)=1:25
A =
1 6 11 16 21
2 7 12 17 22
3 8 13 18 23
4 9 14 19 24
5 10 15 20 25
>> A([1,end],[1,end])
ans =
1 21
5 25

Sign in to comment.

More Answers (5)

mayank ghugretkar
mayank ghugretkar on 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);
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..

Sign in to comment.

Matt J
Matt J on 6 Feb 2019
Edited: Matt J on 6 Feb 2019
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)
top_left =
top_right =
bottom_left =
bottom_right =
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
Amos Agbetile
Amos Agbetile on 7 Feb 2019
yes, thanks. I think that's the problem. I'm supposed to use the function to run the following lines of code in the command window:
A = randi(100,4,5)
[top_left, top_right, bottom_left, bottom_right] = corners(A)
A = [1; 2]
[top_left, top_right, bottom_left, bottom_right] = corners(B)
any suggestions please?

Sign in to comment.

TADA on 6 Feb 2019
Edited: TADA on 6 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
harish kolla on 16 Jun 2019
function [top_left,top_right,bottom_left,bottom_right]= corners (A)
top_left= A(1,1)
top_right= A(1,end)
Stephen23 on 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:

Sign in to comment.

nor el houda bouhaddoun
nor el houda bouhaddoun on 19 May 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);
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
  1 Comment
Stephen23 on 19 May 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 ?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by