How can I create a function that inputs a row
이전 댓글 표시
I need to create a function which takes the following input
- a row
im extremely confused and no data is required to answer this right now.
do i put
댓글 수: 4
madhan ravi
2018년 11월 19일
what have you tried so far? upload your code
madhan ravi
2018년 11월 19일
m2 = vec2mat ([1:25],5) %why is this used?
Please, do not close questions for which you have received extensive feedback (and help). Talking of rudeness, that is particularly rude to those who spent time trying to help you.
edit: and removing all the meaningful part of your question is only going to anger us (and we'll ask Mathworks to restore your question anyway) and unlikely that you'll receive any help in the future. Akcnowledging that you've received help and are willing to learn from it, on the other hand, make it more likely that we'll still help you.
Fope Ayo
2018년 11월 19일
답변 (3개)
Luna
2018년 11월 19일
Hi,
Your return value should be written like below for functions.
Your inputs should be written inside the paranthesis.
function rowSum = myMatrixFunc(A,rowNumber)
rowSum = sum(A(rowNumber,:));
end
댓글 수: 6
Fope Ayo
2018년 11월 19일
Stephen23
2018년 11월 19일
"...but when recalling the function, how do i enter the letter to put in the output "
What is "the letter to put in the output" ?
Fope Ayo
2018년 11월 19일
madhan ravi
2018년 11월 19일
G = [1 2 3; 4 5 6; 7 8 9] %correct usage
Result=myMatrixFunc (G, 1:3) %function calling
function rowSum = myMatrixFunc(A,rowNumber) %function definition
rowSum = sum(A(rowNumber,:));
end
Fope Ayo
2018년 11월 19일
A(rownumber, :) is a row of A. So summing that is going to be the sum for a row. If you want the sum for a column, (i.e. sum across all the rows), then:
function colSum = myMatrixFun(A, colNumber)
colSum = sum(A(:, colNumber));
end
If your definition of the sum for row (or column) differs from the above then you need to explain clearly what it is.
Dennis
2018년 11월 19일
function res=myfunc(matrix,row)
%function [output1,output2]=functionname(input1,input2)
res=sum(matrix(row(row,:)));
end
댓글 수: 6
Fope Ayo
2018년 11월 19일
madhan ravi
2018년 11월 19일
what equation??
Fope Ayo
2018년 11월 19일
Dennis
2018년 11월 19일
After you saved the function and added it to matlabs search path you can call the function from the command line or any other function/script.
Example:
A=randi(100,25,10); %matrix
mysum=myfunc(A,5)
Fope Ayo
2018년 11월 19일
Dennis
2018년 11월 19일
Please check the link in my answer and learn the basics about functions in matlab.
The names of input/output arguments inside a function have nothing to do with variablenames outside that function. 'res' only exists in myfunc, it is the output that function provides.
When you call myfunc you can provide a variable where you want to store the output, but this is completly independant from any variable names used inside the function.
Fope Ayo
2018년 11월 19일
0 개 추천
댓글 수: 2
Guillaume
2018년 11월 19일
The answers did show you the correct syntax. In addition, Dennis' one gave you a link to the documentation that clearly explains how to write functions. Then you come back with:
function [matrix,rowNumber]=myfunc2 (matrixrow)
which has inputs where outputs should be and outputs where inputs should be, so clearly you've failed to grasp even the basics of writing functions. Your further questions showed that you do not understand how to call functions since in one of your comments, you provide 3 inputs to a function that only takes 2.
We don't mind helping you but at some point you need to read the documentation to learn the basics.
Fope Ayo
2018년 11월 19일
카테고리
도움말 센터 및 File Exchange에서 MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!