简体   繁体   English

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

[英]logical operation within vector range expression in MATLAB

can I have something like 我可以有类似的东西吗

A=1:10;

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

meaning I want to zero out specific ranges within my vector index expression in one line 意思是我想在一行中将向量索引表达式中的特定范围归零

Is that possible? 那可能吗?

And what if I wanted to zero out all the rest like 如果我想将其余所有零归零

A(~[1:2]) = 0 

What's the way of logical NOT within vector indexing? 向量索引内逻辑非的方式是什么?

Thanks 谢谢

The following should work: 以下应该工作:

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

If you want to zero the complement of the vector of indices: 如果要使索引向量的补码为零:

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

Where ismembc is a faster, lightweight version of ismember that assumes the array is sorted and non-sparse with no NaN elements. ismembc是更快的,轻量级版本ismember是假设数组进行排序,并没有非稀疏NaN元素。 (Credit goes to this question .) (信用是这个问题 。)

Just do A([1:2 5:6]) . 只要做A([1:2 5:6]) Ie, just create a vector of the indices you want to zero out. 即,只需创建要归零的索引向量即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM