How to have an empty space in a vector for operations
35 views (last 30 days)
Show older comments
Alfredo Scigliani
on 23 Apr 2022
Commented: Alfredo Scigliani
on 23 Apr 2022
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 Comments
Accepted Answer
Matt J
on 23 Apr 2022
Edited: Matt J
on 23 Apr 2022
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')
More Answers (0)
See Also
Categories
Find more on NaNs in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!