Matrix calculation without a For loop

조회 수: 1 (최근 30일)
H R
H R 2017년 5월 1일
답변: Roger Stafford 2017년 5월 1일
Hi. Suppose A is a 1D matrix of [1 by N], and D and C are two square matrix of [N by N]. I would like to calculate D and C without a loop such that each element of D and C are calculated using elements of matrix A:
C(i,j)=A(i)*A(j)
D(i,j)=(A(i)+A(j))/C(i,j)
for instance:
C(1,1)=A(1)*A(1)
C(1,2)=A(1)*A(2)
C(1,3)=A(1)*A(3)
etc.
Thank you.

채택된 답변

Roger Stafford
Roger Stafford 2017년 5월 1일
Given the assumption that A is 1 by N, do this:
C = A(:)*A;
D = bsxfun(@plus,A(:),A)./C;

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by