Issue with writing a function

Hi, I am attempting to write a function (named mean) in order to
1) filter matrix A (100 x 2 matrix) by x & y (i.e all the rows that have x = 1, and y = 2)
--> this will return me with a logical with all the rows that fulfill the criteria of my input of x & y
2) then find out the location of rows which the logical is true (i.e. 1)
3) with the locations, to find out the data that corresponds to the corresponding row numbers in matrix D (logical as true)
4) find mean
My function currently looks something like this:
%%%%%%%%%%%%%%%%%%%%%%%
%% [location] = mean (x,y)
%% xy = A(:,1)==x & A(:,2)==y
%% C = find(xy)
%% D(C) = ans
%% mean(ans)
end
After writing, I wrote in the command window: x = 1; y = 2; function[mean] = mean(x,y) but was returned with an error "function definition not supported in this context. Create functions in code file." I am not sure if my understanding of function writing is accurate and would need help in correcting. Thank you :)

8 Kommentare

Image Analyst
Image Analyst am 2 Sep. 2019
Do NOT name your function mean because it's the name of a very important built-in function that you do not want to clobber. All your "function" has is an end statement. You need to remove the %% in front of the code or else it won't run. Attach your m-file, or both if you're using two.
Cside
Cside am 2 Sep. 2019
ok I have changed it. Attached the m-file too - labelled differently from question for ease
function result = neuron1mean(dataset_pfc_tar_57_n1, rowsncolumns, x, y)
xy = rowsncolumns(:,1)==x & rowsncolumns(:,2)==y;
result = mean(dataset_pfc_tar_57_n1(xy));
end
Cside
Cside am 2 Sep. 2019
Bearbeitet: Cside am 2 Sep. 2019
I labelled the function as neuron1mean and wrote this in the command:
x = 2;
y = 2;
[result] = neuron1mean(x,y)
but there was an error "not enough input arguements" & "Error in neuron1mean (line 2)
xy = rowsncolumns(:,1)==x & rowsncolumns(:,2)==y;"
How may I correct it?
Stephen23
Stephen23 am 2 Sep. 2019
Bearbeitet: Stephen23 am 2 Sep. 2019
You need to call the function without the .m
Basic MATLAB concepts, such as how to call functions, are explained in the introductory tutorials:
Cside
Cside am 2 Sep. 2019
edited the comment! :)
Stephen23
Stephen23 am 2 Sep. 2019
Bearbeitet: Stephen23 am 2 Sep. 2019
"not enough input arguements"
You did not provide enough input arguments when calling the function.
As you did not post the exact code you tried, we have no idea how many input arguments your function has. Here is the first line of Walter Roberson's function:
function result = neuron1mean(dataset_pfc_tar_57_n1, rowsncolumns, x, y)
% ^^^^^^^^^^^^^^^^^^^^^ 1st input
% ^^^^^^^^^^^^ 2nd input
% ^ 3rd input
% ^ 4th input
This is how you called the function (based on your comment):
[result] = neuron1mean(x,y)
As you can see, Walter Roberson's function requires four inputs, but apparently you only provided two. You should check which function you are calling, and check how many input arguments it has.
Cside
Cside am 2 Sep. 2019
i got it! Thank you so much, all of you :) really appreciate the help and patience

Antworten (0)

Diese Frage ist geschlossen.

Gefragt:

am 2 Sep. 2019

Geschlossen:

am 20 Aug. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by