Writing a code in matlab for n variables.

Click For Summary

Discussion Overview

The discussion revolves around writing a MATLAB function to calculate the average of a variable number of inputs. Participants explore different coding approaches and the use of built-in functions for this task.

Discussion Character

  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant shares an initial attempt at writing a function for averaging five variables and seeks guidance on extending it to n variables.
  • Another participant suggests passing a vector to the function and provides a sample implementation that calculates the average using a loop.
  • A third participant proposes a more concise method using the built-in functions, specifically mentioning the use of the mean function.
  • One participant humorously points out that the assignment to write an averaging function may be redundant since MATLAB already has a built-in mean function that performs this task efficiently.
  • The same participant shares the code for the built-in mean function, highlighting its handling of different input dimensions.

Areas of Agreement / Disagreement

Participants generally agree on the advantages of using built-in functions for efficiency, but there is no consensus on the necessity of writing a custom function for averaging.

Contextual Notes

Some limitations include the initial assumption that the user needs to write a custom function despite the existence of built-in alternatives, and the discussion does not address potential edge cases or specific requirements for the averaging function.

Who May Find This Useful

New MATLAB users, students working on assignments involving function writing, and those interested in optimizing code performance in MATLAB.

mmmboh
Messages
401
Reaction score
0
Hi, I am very new to matlab, this is actually the first time I am using it. I have to write a code for finding averages, right now I have "function av=average(x1,x2,x3,x4,x5)
% average(x1,x2,x3,x4,x5) returns the average of (x1,x2,x3,x4,x5)
av=(x1+x2+x3+x4+x5)/5;
return;"
This works just fine when there are 5 things to average, but I can't figure out how to make it work for n variables. Can someone help me please?
 
Physics news on Phys.org
The way this is normally done is to pass x as a vector. So your function looks roughly like this:
Code:
function av = average(x)
[INDENT]n = length(x);
av=0;
for i=1:n
[INDENT]av=av+x(i)/n;[/INDENT]
end[/INDENT]
end

To call that function you use a vector. E.g.
Code:
average([1 3 5 9])
.
 
return sum(A)/length(A)
 
Yeah, in practice you want to always do it as xcvxcvvc suggests - always use the built in functions when possible, because they are usually memory optimized and sometimes numerically optimized. Even better, MATLAB has a function to compute the mean: mean(x).
 
JeSuisConf said:
Yeah, in practice you want to always do it as xcvxcvvc suggests - always use the built in functions when possible, because they are usually memory optimized and sometimes numerically optimized. Even better, MATLAB has a function to compute the mean: mean(x).

hahaha! that's awesome. so this guy has an assignment to write code that a built in MATLAB function already does.

edit:
here is the built in mean() code:

Code:
if nargin==1, 
  % Determine which dimension SUM will use
  dim = min(find(size(x)~=1));
  if isempty(dim), dim = 1; end

  y = sum(x)/size(x,dim);
else
  y = sum(x,dim)/size(x,dim);
end
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
Replies
6
Views
10K