Why am i getting length(x) as 11 instead of 16?

1 回表示 (過去 30 日間)
john
john 2022 年 3 月 13 日
回答済み: Image Analyst 2022 年 3 月 13 日
n=0:15;
  1 件のコメント
Walter Roberson
Walter Roberson 2022 年 3 月 13 日
x and x1 are not the same

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

回答 (2 件)

Torsten
Torsten 2022 年 3 月 13 日
If you display
length(x1)
the answer should be 16.

Image Analyst
Image Analyst 2022 年 3 月 13 日
What did you define for x? All we see are definitions for n and x1:
n=0:15;
% Compute x1
x1 = 1 .* (n >= 1) & (n <= 10)
x1 = 1×16 logical array
0 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0
x1Length = length(x1) % Display it's length.
x1Length = 16
% Now define some x - a different variable than x1
x = [1,2,3,4] % Or whatever it might be.
x = 1×4
1 2 3 4
xLength = length(x) % Display it's length.
xLength = 4
I bet you had an x hanging around from a prior run and didn't call "clear all" so when you did length(x) you were getting some x that was still in the base workspace even though it was not in your script. Calling clear all would clear all old variables and prevent a problem like that.

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by