Matlab function to take matrix as input and return elements in its four corners as output
조회 수: 29 (최근 30일)
이전 댓글 표시
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
Jonathan Deepak
2020년 10월 1일
편집: DGM
2023년 2월 12일
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
채택된 답변
Matt J
2019년 2월 7일
편집: Matt J
2019년 2월 7일
Maybe this is what you really want:
function Acorners = corners(A)
Acorners=A([1,end],[1,end]);
end
댓글 수: 7
ANSHUMAN SARTHAK MEHER
2020년 9월 19일
Acorners=A([1,end],[1,end]); how this helped us? can you please make me understand
Matt J
2020년 9월 19일
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
추가 답변 (5개)
mayank ghugretkar
2019년 6월 3일
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
Matt J
2019년 2월 6일
편집: Matt J
2019년 2월 6일
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
Jos (10584)
2019년 2월 7일
편집: Jos (10584)
2019년 2월 7일
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
TADA
2019년 2월 6일
편집: TADA
2019년 2월 6일
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
댓글 수: 0
harish kolla
2019년 6월 16일
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
ilker melik
2020년 5월 3일
편집: ilker melik
2020년 5월 3일
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
2020년 6월 7일
"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
2020년 5월 19일
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
Stephen23
2020년 5월 19일
@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
2024년 6월 6일
For sake of curiosity:
randi() was introduced to the base toolbox in R2008b, so for it to be missing in 2020 would be curious.
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!