This question is closed. Reopen it to edit or answer.

Issue with writing a function

1 view (last 30 days)
Cside on 2 Sep 2019
Closed: MATLAB Answer Bot on 20 Aug 2021
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)
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 :)
Cside on 2 Sep 2019
i got it! Thank you so much, all of you :) really appreciate the help and patience

Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by