How to choose all other indices than ones I know?
    3 views (last 30 days)
  
       Show older comments
    
Hello.
If have an array
a = [11 12 13 14 15]
and I know that I want to change every other indice to NaN apart from indices 2 and 3. How do I do this?
I tried
a(~[2,3]) = NaN;
but it does not do anything. Thanks!
0 Comments
Accepted Answer
More Answers (2)
  Jos (10584)
      
      
 on 7 Mar 2018
        Here are two options
a = [11 12 13 14 15]
i = [2 3]
b1 = nan(size(a))
b1(i) = a(i)
b2 = a
i2 = setdiff(1:numel(a),i)
b2(i2) = nan
0 Comments
  Jan
      
      
 on 7 Mar 2018
        While setdiff is the nicer solution, it has a remarkable overhead. Logical indexing is usually faster:
a            = 1:10;
index        = true(size(a));
index([2,3]) = false;
a(index)     = NaN;
0 Comments
See Also
Categories
				Find more on Creating and Concatenating Matrices 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!