Creating a table in matlab based on 2 variables and a for loop if statement.
Show older comments
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

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
Answers (1)
Jos (10584)
on 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]
Categories
Find more on Common Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

