Loops, indexing and calling other functions (for the given code)

1 次查看(过去 30 天)
I'm trying to write a function that recalls another function to do the calculations. The values of x and t are already being read from a saved 1x61 vector. z and y can vary and must be specified by the user for a given location. The value of z should be set equal to zero for all cases and doesnt change.
y on the other hand should vary and I want to specify its range say [0 100] but I don't know how to express z and y in the code, since both x and t have dimensions of 1x61 and are indexed.
I need help defining y and z in the below code! Also, please if the current code needs any modifications?
This is what I've done so far:
function [c, tm] = testing( x,y,z,t )
% This function computes concentration for a given time and location
for i=1:length(t)
for j=1:length(x)
z=0;
for y=0:100; %(k index????)
X=x(i);
T=t(j);
[c(i),tm(i)]=Concentration(X,y,z,T);
end
end
end
end
  1 个评论
Stephen23
Stephen23 2015-1-8
Do not use any of the following names for variables or functions:
  • i or j, as these are both names of the inbuilt imaginary unit .
  • profile, as this is the name of the inbuilt execution time tool profile .

请先登录,再进行评论。

回答(1 个)

Stephen23
Stephen23 2015-1-8
The answer to your question really depends on the function profile (please rename this!), which you do not tell us anything about. Can you add a comment and upload the function? It may be that you can vectorize most/all of your code, which might resolve your concerns.
Due to the indexing that you mention, both X and T are scalars, so what is the problem with z and y being scalars too? You state that you "need help defining y and z", but you do not tell us what is the problem with them now.
Defining z and y to be scalars is also unlikely to be a problem even if X and T were arrays, as in MATLAB you can perform many mathematical operations by a scalar on an array of any dimensions. For example:
>> [1,2,3] * 5
ans = [5,10,15]
  4 个评论
Summer
Summer 2015-1-9
"Concentration" is a function defined in another .m file, which "testing" calls to perform the calculations.
Stephen23
Stephen23 2015-1-12
Does the function Concentration accept combinations of scalars and arrays as its inputs? Can you post the code for this function?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by