How do I calculate the cross product of two vectors, when the first vector is an element of a N-array and the second vector is an element of an NxD matrix;
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
% The N-array vector initialization:
r1=zeros(i,:); % i: varies from 0 to 360
% The NxD matrix initialization:
r2=zeros(i,j,:); % i: varies from 0 to 360 and j: varies from 10 to 250
% A double for loop is used and it contains the cross product of the
% two:
for i=0:1:360
for j=10:1:250
c=cross(r1(i,:),r2(i,j,:))
end
end
% The error is that Matrices are not of the same size.
0 commentaires
Réponses (1)
Torsten
le 2 Mar 2024
Déplacé(e) : Torsten
le 2 Mar 2024
r1 = rand(30,3);
r2 = rand(30,50,3);
for i=1:30
for j=1:50
c(i,j,:)=cross(r1(i,:),squeeze(r2(i,j,:)));
end
end
4 commentaires
Paul
le 2 Mar 2024
cross can work on multiple vectors, so a single loop can be used, for whatever that's worth
rng(100)
r1 = rand(30,3);
r2 = rand(30,50,3);
for i=1:30
for j=1:50
c(i,j,:)=cross(r1(i,:),squeeze(r2(i,j,:)));
end
end
for j = 1:50
cc(:,j,:) = cross(r1,squeeze(r2(:,j,:)),2);
end
Or no loop using some extra memory, also for whatever that's worth.
ccc = cross(repmat(reshape(r1,30,1,3),[1 50]),r2);
isequal(c,cc,ccc)
Voir également
Catégories
En savoir plus sur Multidimensional Arrays dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!