MATLAB 中向量范围表达式内的逻辑运算

logical operation within vector range expression in MATLAB

我能有类似的东西吗

A=1:10;

A(1:2 && 5:6)=0;

意思是我想在一行中将向量索引表达式中的特定范围归零

这可能吗?

如果我想像

一样将所有其余部分清零怎么办?
A(~[1:2]) = 0 

向量索引中逻辑非的方式是什么?

谢谢

以下应该有效:

idx = [1:2,5:6];
A(idx) = 0

如果要将索引向量的补码归零:

idx = [1:2,5:6];
A(~ismembc(1:length(A),idx)) = 0

其中 ismembc is a faster, lightweight version of ismember that assumes the array is sorted and non-sparse with no NaN elements. (Credit goes to this question.)

就做A([1:2 5:6])。即,只需创建一个要清零的索引向量。