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;

2 visualizzazioni (ultimi 30 giorni)
% 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.

Risposte (1)

Torsten
Torsten il 2 Mar 2024
Spostato: Torsten il 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 Commenti
Paul
Paul il 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)
ans = logical
1

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by