Writing a code in matlab for n variables.

In summary, the conversation discusses a user's need to create a code for finding averages in MATLAB and the suggestion is to use a vector and built-in functions such as mean() for more efficiency. The built-in mean() function is shown as an example.
  • #1
mmmboh
407
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
  • #2
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])
.
 
  • #3
return sum(A)/length(A)
 
  • #4
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).
 
  • #5
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
 

What is Matlab?

Matlab is a programming language and software environment commonly used in scientific and engineering fields for data analysis, visualization, and algorithm development.

What is the purpose of writing code in Matlab for n variables?

The purpose of writing code in Matlab for n variables is to efficiently manipulate and analyze large sets of data and perform complex calculations and simulations. It allows for quick and accurate processing of data and provides a user-friendly interface for data visualization.

What are the key components of a Matlab code for n variables?

The key components of a Matlab code for n variables include variable declaration, mathematical operations, conditional statements, loops, and function calls. These components work together to perform the desired calculations and manipulations on the data.

What are the common challenges when writing a code in Matlab for n variables?

Some common challenges when writing a code in Matlab for n variables include debugging errors, managing memory and processing time for large datasets, and optimizing the code for efficiency. It is important to plan and organize the code before writing to avoid potential challenges.

What are some tips for writing an efficient code in Matlab for n variables?

To write an efficient code in Matlab for n variables, it is important to use vectorized operations, preallocate memory, avoid unnecessary loops, and use built-in functions whenever possible. It is also helpful to break the code into smaller, reusable functions for better organization and readability.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
14
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
876
  • Engineering and Comp Sci Homework Help
Replies
1
Views
509
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
986
  • Precalculus Mathematics Homework Help
Replies
3
Views
3K
  • Classical Physics
Replies
4
Views
887
Back
Top