Loops, indexing and calling other functions (for the given code)
Afficher commentaires plus anciens
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 commentaire
Stephen23
le 8 Jan 2015
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 .
Réponses (1)
Stephen23
le 8 Jan 2015
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 commentaires
Summer
le 8 Jan 2015
Why are you concerned that z and y are 1x1 arrays (aka scalars)? You have not explained anywhere why this might be a problem: are you getting some error, or a wrong result?
How are you trying to "increment" y ? In you code the values of y is incremented automatically in the for loop definition that you use: this value is a scalar. In fact, in MATLAB the for loop value is always a scalar, they do not increase in size as the loop iterates. Please read the documentation. If you need y to be something other than a scalar, then you need to write the code to generate this, perhaps based on the loop variable.
Please indicate exactly the values that you require for the first two or three loop iterations. Then we can figure out the best ways for you to generate these values.
Can you please tell us about "Concentration": is it an array, or a function? Knowing how this behaves is critical to being able to help you: please post a comment giving the exact code or definition of this.
Summer
le 9 Jan 2015
Stephen23
le 12 Jan 2015
Does the function Concentration accept combinations of scalars and arrays as its inputs? Can you post the code for this function?
Catégories
En savoir plus sur Matrix Indexing dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!