Creating a table in matlab based on 2 variables and a for loop if statement.

11 visualizzazioni (ultimi 30 giorni)
Hi all,
Any help would be great as I am completely stuck. I am trying to create a table/array/structure (whatever is best for this) based on 2 variables X&Y if a certain condition is met and plot a time vs Z value.
To calculate Z(1):
If the value of X(1) is greater than Time then add the value of Y(1) - linear growth after Time goes past the X value. Then I want it to loop to get Z(2) and so on.
Sorry if that is badly explained but the table below is the output I am looking for.
Thanks
Capture.PNG
Number = 5;
for i = 1:Number
X(i) = rand();
Y(i) = rand();
end
for j = 1:Number
for time=1:100
if t(time)<X(j)
Z(time)=0;
else
Z(time) = Z(time)+Y(j);
end
end
end

Risposte (1)

Jos (10584)
Jos (10584) il 7 Mar 2019
This creates your table
N = 11 ; % 10 timestamps
time = 0:N-1 ;
x = [4 3 1 4 2] ;
y = [1 2 3 1 4] ;
Z = repmat(y, N, 1)
for k=1:5
Z(1:x(k),k) = 0
end
Z = cumsum(Z,1)
Ztotal = sum(Z,2)
Yourtable = [time(:) Z Ztotal]
  1 Commento
Podge
Podge il 11 Mar 2019
Thanks for the response. Even though this was the exact table I was looking for its not really what I am trying to achieve. I am trying to plot time vs Z value from 0-(the amount of time to hit Z max).
As an example if my variables are:
X = [4, 3, 1, 4, 2]
Y= [1, 2, 3, 1, 4]
Z max = 96
(these variables will be different for each study, just using them as an example of what I am trying to achieve).
The value of Z then depends on when time is greater or equal to the x value. if it is greater then Z starts populating by the y value.
in the loop I am trying to calculate all Z values until the Ztotal value >=Zmax.
So the first loop will look like this:
Capture.PNG
2nd loop will look like this:
Capture.PNG
you can see z3=3 for this. This is because the x value is now >= time.
so the code in my head for this would be:
if x(3)>time then starting poltting y values. I can do this by writing a really long code but think it could be acieved in a few lines of code in a loop (just cant figure it out).
After time=10 the Zmax value of 96 is hit. I want to loop to stop here.
Capture.PNG
Hopefully this is explanied a bit better and I really appreciate your help so far.
Thanks again.

Accedi per commentare.

Prodotti


Release

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by