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?

 Réponse acceptée

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 commentaires

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
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
thank you so much. Because i have see that it works also with " ' " as well as " .' "
Voss
Voss le 2 Mar 2022
" .' " (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'

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Mathematics dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by