MATLAB Answers

Incorporate Incremental Vector into an Equation

조회 수: 2(최근 30일)
Loren Smitley
Loren Smitley 30 Oct 2020
답변: Peter O 30 Oct 2020
How would I incorporate an incremental vector into an equation for calculation at every increment of that vector?

  댓글 수: 0

댓글을 달려면 로그인하십시오.

답변(1개)

Peter O
Peter O 30 Oct 2020
Use the elementwise operators (preceed divide/multiply with a dot).
https://www.mathworks.com/help/matlab/matlab_prog/array-vs-matrix-operations.html
For instance:
x = 1:100; % Array of 1 to 100
y = sin(x).*cos(2*x)./tan(x).^2; % Applies the functions along the elements individually.
Addition and subtraction don't need the periods, but you'll have to ensure that the vectors you're combining have the same size. If you're using a scalar in an operation, MATLAB will automatically broadcast it across the array (see the 2 in the cosine term).
Any functions you call and pass the vector will need to be able to operate on a vector (most built-ins will), or you can use arrayfun() to wrap the function and apply it to each value indivudally. (Or use a for loop). The syntax is:
z = arrayfun(@(x) my_scalar_fun(x), y)

  댓글 수: 0

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by