How to have an empty space in a vector for operations

36 次查看(过去 30 天)
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 NaNs 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by