How can I create a function that inputs a row

7 次查看(过去 30 天)
I need to create a function which takes the following input
  1. a row
im extremely confused and no data is required to answer this right now.
do i put
  4 个评论
Guillaume
Guillaume 2018-11-19
编辑:Guillaume 2018-11-19
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.

请先登录,再进行评论。

回答(3 个)

Luna
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
Fope Ayo 2018-11-19
THANK YOU SO MUCH!
BUT this does not work for my question completely.
I need to calculate the sum for a row not the column, could you correct that for me if possible?
Guillaume
Guillaume 2018-11-19
编辑:Guillaume 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
Dennis 2018-11-19
You can read about functions here.
function res=myfunc(matrix,row)
%function [output1,output2]=functionname(input1,input2)
res=sum(matrix(row(row,:)));
end
  6 个评论
Dennis
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
Fope Ayo 2018-11-19
the answers did not fix my ‘buggy’ function. is there a need to be so rude?
  2 个评论
Guillaume
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
Fope Ayo 2018-11-19
No this is the first time i've used matlab so yes I am just learning, i have read the basics but it was a little confusing when relating it to my question. Nevertheless, thanks for the help.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息

标签

尚未输入任何标签。

Community Treasure Hunt

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

Start Hunting!

Translated by