transform vector in 2 dimensions into one in 3

for i = 1:duration
traci.simulation.step();
for n = 1: length(inductionID)
veicoliOGNIISTANTEsuInduction(n,i)=traci.inductionloop.getLastStepVehicleNumber(inductionID{n});
end
for v=1:length(k)
if i==t(v)
for n = 1: length(inductionID)
veicoliT(n,v)=sum(veicoliOGNIISTANTEsuInduction(n,(t(v)-(T-1)):t(v)));
end
newgreentime(:,v)=L*veicoliT(:,v); % L is a constant matrix
end
end
hi all, i have a vector n rows by v (whose columns are indexed). my vector newgreentime (:, v) in the considered v becomes a vector 4 x1, I would like to use this vector in a 3 dimensional vector 2x2xv how can I do it?

 採用された回答

Walter Roberson
Walter Roberson 2022 年 3 月 1 日

0 投票

newgreentime(:,:,v) = reshape(L*veicoliT(:,v), 2, 2); % L is a constant matrix
This assumes that you want the order
A B C D ->
[A C
B D]
if you need
[A B
C D]
then
newgreentime(:,:,v) = reshape(L*veicoliT(:,v), 2, 2).'; % L is a constant matrix

4 件のコメント

Marco Carapellese
Marco Carapellese 2022 年 3 月 1 日
thank you very much for the answer. A curiosity why to "reshape (L * vehiclesT (:, v), 2, 2). '" Why is there also that point near the closing parenthesis? it seems to work for what I want, however as soon as I can fully verify, thanks again
Walter Roberson
Walter Roberson 2022 年 3 月 2 日
You have a vector of 4 elements. Compare
v = [1;2;3;4]
v = 4×1
1 2 3 4
reshape(v,2,2)
ans = 2×2
1 3 2 4
reshape(v,2,2).'
ans = 2×2
1 2 3 4
If you want adjacent elements in the vector to form columns, then do not use the .' but if you want adjacent elements in the vector to form rows, then you need the .' operator.
The .' operator is formally known as transpose:
transpose(reshape(v,2,2))
ans = 2×2
1 2 3 4
In general, if you have a vector v and you want to form an A by B matrix out of it, your choices are
reshape(v, A, B) %go down the columns
reshape(v, B, A).' %go across the rows
Marco Carapellese
Marco Carapellese 2022 年 3 月 2 日
thank you so much. Because i have see that it works also with " ' " as well as " .' "
Voss
Voss 2022 年 3 月 2 日
" .' " (dot single-quote) is transpose.
" ' " (single-quote) is complex conjugate transpose.
If the thing you are transposing (say X) contains only real values (i.e., no complex numbers), then X.' is the same as X'

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMathematics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by