필터 지우기
필터 지우기

I want to create a n by 1 matrix. The FDF2=2 is on the top row, while BDF2=3 is on the bottom. And the values of the middle row should all equal to 3. How do I input the values in such matrix?

조회 수: 1 (최근 30일)
Should I create a zeros(n+1,1) matrix first?

채택된 답변

John D'Errico
John D'Errico 2017년 4월 17일
편집: John D'Errico 2017년 4월 17일
I'm not sure why you would initially create a VECTOR of length n+1, if your final goal is an nx1 vector. :)
No. There is no need to preallocate the vector with zeros, although your mentioning that idea is the only reason I was willing to answer your question. There are lots of ways to create this vector though. A simple one is:
V = [FDF2;repmat(3,n-2,1);BDF2];
An alternative is to preallocate a vector, then stuff the first and last elements.
V = ones(n,1)*3;
V([1 end]) = [FDF2,BDF2];
Lots of other ways to do this, but that covers the first ones I might think of. You can see that the first solution involves concatenation of the indicated elements, whereas the second had me create a vector of the final size, then stuffing the first and last elements as desired. Either is fine.
  댓글 수: 2
kingsley
kingsley 2017년 4월 17일
편집: kingsley 2017년 4월 17일
got it! thank you. I'm new to mathlab. So the question I ask might sound stupid. But what if the value on the on the middle rows. And it has to be 2*fi, where i is from 1 to n-1, and the function of f=sin(i)? Do I need to write a loop in that case ?
John D'Errico
John D'Errico 2017년 4월 19일
Learn to think in terms of vectors. What is
1:(n-1)
This is a row vector. But if you want a column vector, then what does this do?
(1:(n-1))'
Can you compute the sine of ALL of those values in one call to sin? Of course you can.
Think at a high level, in terms of vectors & arrays, not in terms of loops.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by