I have an array of size 3 x 100 and I basically want to create a new array where I take each column vector in the array and compute the cross product with the same vector each time to create another array of size 3 x 100, so in this case I take every vector and form the cross product with [0 0 1]'. What would be the easiest way of doing this?

 채택된 답변

James Tursa
James Tursa 2019년 4월 22일
편집: James Tursa 2019년 4월 22일

1 개 추천

M = your 3xN matrix
v = your 3x1 vector
result = cross(M,repmat(v,1,size(M,2)));

댓글 수: 10

Hollis Williams
Hollis Williams 2019년 4월 22일
OK, thanks. Also I realise I have made a mistake elsewhere in the code. Say you have that that array of N column vectors in the 3 x 100 array and you want to normalise each vector in the array, so I just want to take each 3 x 1 vector and then divide it by its norm, how would I best do that?
James Tursa
James Tursa 2019년 4월 22일
편집: James Tursa 2019년 4월 22일
result = M./sqrt(sum(M.*M));
Or, for older versions of MATLAB:
result = bsxfun(@rdivide,M,sqrt(sum(M.*M)));
Hollis Williams
Hollis Williams 2019년 4월 22일
Your command for cross product does not seem to work, as it seems like the two inputs for the cross product are not the same size: if M is 3x100, say, repmat(v,1,size(M,2)) is of size 3x300.
James Tursa
James Tursa 2019년 4월 22일
편집: James Tursa 2019년 4월 22일
My code specifically calls out for v to be a 3x1 column vector. If your vector is a 1x3 row vector, then simply transpose it to be a column vector. Or use the colon operator to force it to a column vector in either case:
result = cross(M,repmat(v(:),1,size(M,2)));
If v is some other size, you will have to tell us what you are really working with.
Hollis Williams
Hollis Williams 2019년 4월 22일
편집: Hollis Williams 2019년 4월 22일
If I try taking the transpose, I seem to get something which is of size 1 x 900 when I run repmat(v,1,size(M,2)).
James Tursa
James Tursa 2019년 4월 22일
What is size(v) and size(M)?
Hollis Williams
Hollis Williams 2019년 4월 22일
size(M)=3 x 100
size(v)=3x1
James Tursa
James Tursa 2019년 4월 22일
편집: James Tursa 2019년 4월 22일
This is what I get:
>> M = rand(3,100);
>> v = rand(3,1);
>> size(repmat(v,1,size(M,2)))
ans =
3 100
I have no idea why you would be getting a 3x300 result. Did you actually run the size(M) and size(v) commands at the MATLAB prompt, or are you just reporting to me what you think they are? Actually type this in and see what you get
size(M)
size(v)
Hollis Williams
Hollis Williams 2019년 4월 22일
Yes, I was using the size() command but made a typo, it is working now. I have created three 1 x 100 arrays, is it possible to put these together into one 3 x 100 array? So if A,B,C are all 1 x 100 arrays, I would need
A
B
C
result = [A;B;C];

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

추가 답변 (1개)

Matt J
Matt J 2019년 4월 22일
편집: Matt J 2019년 4월 22일

0 개 추천

This way avoids repmatting, which may be desirable when N is large.
M = your 3xN matrix
v = your 3x1 vector
result=xprodmat(v)*M./vecnorm(M);
where
function A=xprodmat(a)
%Matrix representation of a cross product
%
% A=xprodmat(a)
%
%in:
%
% a: 3D vector
%
%out:
%
% A: a matrix such that A*b=cross(a,b)
if length(a)<3, error 'Input must be a vector of length 3'; end
ax=a(1);
ay=a(2);
az=a(3);
A=zeros(3);
A(2,1)=az; A(1,2)=-az;
A(3,1)=-ay; A(1,3)=ay;
A(3,2)=ax; A(2,3)=-ax;
end

댓글 수: 1

Hollis Williams
Hollis Williams 2019년 4월 22일
I don't think repmatting is the thing which is most computationally costly in my code but I will bear this in mind to see if I need to save time later on, thanks a lot.

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

카테고리

도움말 센터File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

제품

릴리스

R2018b

질문:

2019년 4월 22일

댓글:

2019년 4월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by