Filter löschen
Filter löschen

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

2 Ansichten (letzte 30 Tage)
Summer
Summer am 8 Jan. 2015
Kommentiert: Stephen23 am 12 Jan. 2015
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 Kommentar
Stephen23
Stephen23 am 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 .

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Stephen23
Stephen23 am 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 Kommentare
Summer
Summer am 9 Jan. 2015
"Concentration" is a function defined in another .m file, which "testing" calls to perform the calculations.
Stephen23
Stephen23 am 12 Jan. 2015
Does the function Concentration accept combinations of scalars and arrays as its inputs? Can you post the code for this function?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by