Vector addition with 2-dim array

조회 수: 3 (최근 30일)
Jeon
Jeon 2013년 3월 21일
A is an R by C (R x C) array and I have a rows vector V with length C. I want to add or subtract V from every rows in the array A.
Is there any way to simplify this procedure?
Of course I know some method like:
VV = V(onese(1:R), :)
result = A +/- VV;
but I just wanted an one-line command or operator, like:
result = A V
result = somefunction(A, V);
I need to find ※ or somefunction()
  댓글 수: 1
Jan
Jan 2013년 3월 21일
ones(1:R) creates a matrix with R dimensions and prod(1:R) elements. You mean ones(1, R).
The one-line command might have the advantage, that the matrix VV is not created explicitly:
result = A - v(ones(R,1), :);

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

채택된 답변

Wouter
Wouter 2013년 3월 21일
you could use this oneliner:
result = A - (ones(size(A,1),1)*v);
  댓글 수: 1
Wouter
Wouter 2013년 3월 21일
and if v is a column vector you need to transpose v:
result = A - (ones(size(A,1),1)*v');

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

추가 답변 (1개)

Jan
Jan 2013년 3월 21일
A = rand(3,4);
b = rand(1,4);
C = bsxfun(@minus, A, b);

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by