- 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 .
Loops, indexing and calling other functions (for the given code)
2 次查看(过去 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
2015-1-8
Do not use any of the following names for variables or functions:
回答(1 个)
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 个评论
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 Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!