How to simplify the given code snippet?

조회 수: 1 (최근 30일)
Lulu
Lulu 2011년 11월 2일
Hello,
I need to simplify the following code. Perhaps 'reshape' function should be used, but in which way? Any ideas would be appreciated.
t=0:0.05:1;
% Dimensions of X and dx are the same
X=1:3; dx=0:0.1:0.3;
% How to write these 4 lines as 1?
colX1 = (X(1) + dx(1)*t)';
colX2 = (X(2) + dx(2)*t)';
colX3 = (X(3) + dx(3)*t)';
colX = [colX1; colX2; colX3];
  댓글 수: 1
Walter Roberson
Walter Roberson 2011년 11월 2일
Note: your dx array might turn out as either 3 or 4 elements long, so be careful not to code anything that depends implicitly upon the length of dx.

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

채택된 답변

Andrei Bobrov
Andrei Bobrov 2011년 11월 2일
colX2 = reshape(bsxfun(@plus,X,bsxfun(@times,t',dx(1:3))),[],1);
or
colX2 = reshape(bsxfun(@plus,X,t'*dx(1:3)),[],1)

추가 답변 (0개)

카테고리

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