Is there a more efficient way to format my vector rather than manually change the code by hand

조회 수: 2 (최근 30일)
%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
Dyuman Joshi
Dyuman Joshi 2022년 8월 16일
편집: Dyuman Joshi 2022년 8월 16일
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)
ans = logical
1

댓글을 달려면 로그인하십시오.

채택된 답변

Steven Lord
Steven Lord 2022년 8월 16일
%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)
ans = logical
1
  댓글 수: 3
Steven Lord
Steven Lord 2022년 8월 16일
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.
Adil Saeed
Adil Saeed 2022년 8월 16일
within my model, Pn represents the total number of impacts however these occur at different rates shown by 'In' and now the vector 'TV' shows this rate, i.e. the first 30 impacts occur once every 30 s or INT/In(1), the next 60 impacts occur once every 15 s or INT/In(2). for each of these impacts t'' has a different value which i need further down in my code. what i had sent up was the code below, but when i change the values for 'T', 'INT' and 'In' these values will not change to cater what was provided above and if we increase 'In' to 10 I would need 9 elseif statements which is not set up below. So what im asking is the same, if we change 'T', 'INT' and 'In' how can this change fix the statements below.
%Time
T = 5400;
INT = 900;
tme = 0:INT:T;
%Number of Positions and Impacts
In = [30, 60, 75, 375, 420, 360];
Pn = sum(In);
PnC = cumsum(In);
n = numel(tme);
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{:}];
for i = 1:Pn
if 0<i && i<=PnC(1)
t = INT/In(1);
elseif PnC(1)<i && i<=PnC(2)
t = INT/In(2);
elseif PnC(2)<i && i<=PnC(3)
t = INT/In(3);
elseif PnC(3)<i && i<=PnC(4)
t = INT/In(4);
elseif PnC(4)<i && i<=PnC(5)
t = INT/In(5);
elseif PnC(5)<i && i<=PnC(6)
t = INT/In(6);
end
%Distance the Mammal has moved
s = 2.35*t;
end

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Types에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by