How to have an empty space in a vector for operations

30 ビュー (過去 30 日間)
Alfredo Scigliani
Alfredo Scigliani 2022 年 4 月 23 日
コメント済み: Alfredo Scigliani 2022 年 4 月 23 日
I would like to have a vector with an blank space, not a NaN, simply a blank space. Because then I need to take the average and I would like for it to not take into account that space, for example:
x = [10 20 30 40 50 70] %(as you can see, it is missing the 60, but it has a length of 7 values)
y = [ 20 40 60 80 100 120 140] %(length of 7 values)
First, would like to know how to write that x vector.
Then I want to take the average of both vectors as:
xy_avg = mean([x,y])
so that it yeilds:
xy_avg = [ 15 30 45 60 75 120 105] %(the 120 was averaged with the blank space and the answer was just 120)
Please, I cannot have it as NaN, it has to be literally empty blank space or position, without reducing the length of the vector.
thanks in advance!

採用された回答

Matt J
Matt J 2022 年 4 月 23 日
編集済み: Matt J 2022 年 4 月 23 日
I would like to have a vector with an blank space, not a NaN, simply a blank space.
There is no such thing as a "blank space" in a matrix or vector. However, inserting a NaN will do what you want if you provide an 'omitnan' flag.
x = [10 20 30 40 50 nan 70]; %(as you can see, it is missing the 60, but it has a length of 7 values)
y = [ 20 40 60 80 100 120 140]; %(length of 7 values)
mean([x;y],'omitnan')
ans = 1×7
15 30 45 60 75 120 105
  1 件のコメント
Alfredo Scigliani
Alfredo Scigliani 2022 年 4 月 23 日
Oh I see, well this works too so thank you so much!!

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by