Is there a more efficient way to format my vector rather than manually change the code by hand
2 views (last 30 days)
Show older comments
%Time
T = 5400;
INT = 900;
tme = 0:INT:T;
%Number of Positions and Impacts
Pn = 1320;
In = [30, 60, 75, 375, 420, 360];
TV = [tme(1):INT/In(1):tme(2)-INT/In(1), tme(2):INT/In(2):tme(3)-INT/In(2), tme(3):INT/In(3):tme(4)-INT/In(3), tme(4):INT/In(4):tme(5)-INT/In(4),...
tme(5):INT/In(5):tme(6)-INT/In(5), tme(6):INT/In(6):tme(7)-INT/In(6)];
I have this code, however is there an easier way to code this if i want to change 'T', 'INT' and 'In'. The number of values in 'In' should be equal to T/INT. So if we did change T = 5000; INT = 500; and the number of values in In = 10, these values can be random however summed should equal 'Pn', how can that 'TV' vector change automatically to cater this change or how can we combine these condition to produce the 'TV' vector
1 Comment
Dyuman Joshi
on 16 Aug 2022
Edited: Dyuman Joshi
on 16 Aug 2022
You can use a for loop
%Time
T = 5400;
INT = 900;
tme = 0:INT:T;
%Number of Positions and Impacts
Pn = 1320;
In = [30, 60, 75, 375, 420, 360];
%pre-allocation
TV=cell(1,numel(tme)-1);
for i=1:numel(tme)-1
TV{i}=tme(i):INT/In(i):tme(i+1)-INT/In(i);
end
TV=cell2mat(TV);
TVman = [tme(1):INT/In(1):tme(2)-INT/In(1), tme(2):INT/In(2):tme(3)-INT/In(2), tme(3):INT/In(3):tme(4)-INT/In(3), tme(4):INT/In(4):tme(5)-INT/In(4),...
tme(5):INT/In(5):tme(6)-INT/In(5), tme(6):INT/In(6):tme(7)-INT/In(6)];
%comparison
isequal(TV,TVman)
Accepted Answer
Steven Lord
on 16 Aug 2022
%Time
T = 5400;
INT = 900;
tme = 0:INT:T;
%Number of Positions and Impacts
Pn = 1320;
In = [30, 60, 75, 375, 420, 360];
Let's define a helper variable.
n = numel(tme);
Based on the pattern of your TV, it seems we need to have In have one fewer element than tme. Let's check that first.
assert(numel(In) == n-1, "In had " + numel(In) + " elements, expected " + (n-1))
Now rather than try to build TV all at once, let's build the individual pieces in cells of a cell array (since they're different lengths) and assemble TV at the end.
TVpieces = cell(1, n-1);
for pn = 1:n-1 % pn = piece number
Inc = INT/In(pn);
TVpieces{pn} = tme(pn):Inc:(tme(pn+1)-Inc);
end
TV = [TVpieces{:}];
Let's check the results.
TVCheck = [tme(1):INT/In(1):tme(2)-INT/In(1), tme(2):INT/In(2):tme(3)-INT/In(2), ...
tme(3):INT/In(3):tme(4)-INT/In(3), tme(4):INT/In(4):tme(5)-INT/In(4),...
tme(5):INT/In(5):tme(6)-INT/In(5), tme(6):INT/In(6):tme(7)-INT/In(6)];
isequal(TV, TVCheck)
3 Comments
Steven Lord
on 16 Aug 2022
cell2mat can turn a cell array containing compatibly-sized "blocks" into a matrix. But in this case I know the cells in the cell array I created are all row vectors. I can take advantage of that knowledge by just doing the concatenating directly rather than having cell2mat figure out that's what it needs to do then do it.
More Answers (0)
See Also
Categories
Find more on Data Types 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!