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!
0 个评论
采纳的回答
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')
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 NaNs 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!