Create a vector that only the first element and the last element is 0

1 回表示 (過去 30 日間)
Ming Ki Lee
Ming Ki Lee 2017 年 1 月 25 日
編集済み: per isakson 2017 年 1 月 25 日
B=zeros(mx,1);
for z=1:mx
if B(z,1) == B(1,1)
B(z,1) = 0;
elseif B(z,1) == B(mx,1)
B(z,1) = 0;
else
B(z,1) = 1;
end
end
I want to create a vector B that only (1,1) and (mx,1) equal to 0 but from (2 to mx,1) equal to 1. But my code still only show a zero matrix. How can I fix that?

回答 (1 件)

KSSV
KSSV 2017 年 1 月 25 日
編集済み: KSSV 2017 年 1 月 25 日
N = 10 ; % length of the vector
iwant = ones(N,1) ;
iwant(1) = 0 ;
iwant(N) = 0 ;
  2 件のコメント
Walter Roberson
Walter Roberson 2017 年 1 月 25 日
Or more compactly,
iwant = [0; ones(N-2,1); 0];
Jan
Jan 2017 年 1 月 25 日
編集済み: Jan 2017 年 1 月 25 日
Or with the loop:
B = zeros(mx,1);
for z = 2:mx-1
B(z) = 1;
end
This is not efficient and I would not use it in real programs, but it shows how your loop can be modified.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDiscrete Fourier and Cosine Transforms についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by