Write a function called corners that takes a matrix as an input argument and returns four outputs: the elements at its four corners in this order: top_left, top_right, bottom_left and bottom_right. (Note that loops and if-statements are neither neces
Info
This question is locked. Reopen it to edit or answer.
Show older comments
This question is soft-locked: new answers that are equivalent to already posted answers may be deleted without prior notice.
Can't find a solution to this problem im a noob, please help, example
>> [a, b, c, d] = corners([1 2; 3 4])
a =
1
b =
2
c =
3
d =
4
12 Comments
Alex Mcaulley
on 23 May 2019
It is an easy problem. What have you tried?
Debaditya Chakraborty
on 23 May 2019
Guillaume
on 23 May 2019
As Alex pointed out, it's an easy problem. I'd go as far as saying that if you don't know how to do that, you haven't even bothered to learn matlab basics. It's trivial indexing and if you can't solve it on your own, you will really struggle with any matlab assignment.
Debaditya Chakraborty
on 23 May 2019
Will I get it running by this function?
The easiest and, by far, the fastest way to know is to try it for yourself rather than waiting for us to tell you.
The answer, however, is no, even not taking into account the syntax error in the function definition.
madhan ravi
on 23 May 2019
No, verify it yourself.
Debaditya Chakraborty
on 23 May 2019
Sejal Syed
on 21 Sep 2019
Edited: Sejal Syed
on 21 Sep 2019
Does not works Tried and done this Jst end the statements with ; so that it doesn't prints a b c d
Sushant Pal
on 4 May 2020
function [top_left, top_right, bottom_left, bottom_right] = corners(x)
[m,n] = size(x);
top_left = x(1,1);
top_right = x(1, n);
bottom_left = x(m,1);
bottom_right = x(m, n);
end
Rajas Patil
on 1 Jun 2020
The function does not get run
SATHISHKUMAR S
on 18 Aug 2020
Thank you very much sir. I learned.
QueenX
on 20 Oct 2020
That's really nice. Thanks a lot!
Answers (4)
Muhammad Barkhaya
on 24 Nov 2019
function [a,b,c,d]=corners(x)
a=x(1,1)
b=x(1,end)
c=x(end,1)
d=x(end,end)
end
3 Comments
Mukundan Rajagopal
on 5 Aug 2020
Thanks!
Luis Paternina Espitia
on 23 Oct 2020
Thanks!
B Akash
on 16 Sep 2021
tnx u soo much ....<3
Yihan Liu
on 22 Sep 2019
Well, this one could work.
function [a,b,c,d]=corners(A)
[m,n] = size(A);
a=A(1,1); % Top left
b=A(1,n); % Top right
c=A(m,1); % Bottom left
d=A(m,n); % Bottom right
end
1 Comment
Guillaume
on 22 Sep 2019
Despite Sejal Syed's comment, the function that Debaditya ended up with does work and is slightly simpler (since it uses the end keyword) instead of querying the size.
ABHIJIT BISWAS
on 22 Nov 2020
Edited: Image Analyst
on 16 Sep 2021
function [a, b, c, d] = corners(x)
a = x(1,1); %top left
b = x(1,end); %top right
c = x(end,1); %bottom left
d = x(end,end); %bottom right
The bottom left and bottom right are the most important part.
Check the 3rd and 4th line of code properly.
Hope this helps!
5 Comments
Mohamad Kinan OTHMAN
on 26 Nov 2020
function [a,b,c,d]=corners(x)
[m,n]=size(x);
a=x(1,1); %top_left
b=x(1,n); %top_right
c=x(m,1); %bottom_left
d=x(m,n); %bottom_right
end
I did like that but it doesn't work, why?
Ahmed MARUF
on 19 Jan 2021
check if it is save it corners.m format
then save it
then give the value of A
Such as
A = randi(100,4,5)
I hope it will work
Image Analyst
on 16 Sep 2021
@Mohamad Kinan OTHMAN, Abhijit's way works fine:
m = randi(100, 6, 4)
[a, b, c, d] = corners(m)
function [a, b, c, d] = corners(x)
a = x(1,1); %top left
b = x(1,end); %top right
c = x(end,1); %bottom left
d = x(end,end); %bottom right
end
This (your way) also works fine:
m = randi(100, 6, 4)
[a, b, c, d] = corners(m)
function [a,b,c,d]=corners(x)
[m,n]=size(x);
a=x(1,1); %top_left
b=x(1,n); %top_right
c=x(m,1); %bottom_left
d=x(m,n); %bottom_right
end
Karan
on 24 Oct 2023
function [top_left,top_right,bottom_left,bottom_right] = corners(rows, columns)
e = rand(rows,columns);
top_left = e(1,1);
top_right = e(1,end);
bottom_left = e(end,1);
bottom_right = e(end,end);
end
That's not an answer to the question that was asked, and it's hard to imagine the question for which it would be a practical solution.
You're supposed to take a given 2D array and return its corner elements. Instead, your function takes two size arguments and returns the corner elements of a random array of that size. In effect, the input arguments to your function are meaningless. The outputs are four random numbers. The fact that they are corner elements is completely inconsequential but in the trivial case of vectors. They're just a set of random numbers.
I think it's fair to say that this question has been exhausted. It's bad enough that people paste the same code over and over as if it's a helpful revelation. It should stand to reason that there's little use for answers that don't acknowledge the task at hand.
Sisay Girma
on 23 Feb 2024
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
% Code to call your function
A = randi(100,2,2)
[top_left, top_right, bottom_left, bottom_right] = corners(A)
2 Comments
Rik
on 23 Feb 2024
What exactly does this answer add to the other answers in this thread? What does it teach? You're more than welcome to start answering questions, but why post a solution to a homework question where equivalent answers already exist?
You might be interested in giving Cody a try if you want to post your own solution for solved questions.
DGM
on 23 Feb 2024
Other than a change of variable name, how is this any different? If it's not demonstrating something different, then why does it exist?
This question is locked.
Categories
Find more on Descriptive Statistics in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!