Circularly exclude elements.
显示 更早的评论
Hello all,
I'm trying to compactly write a line of code for a interesting program I'm working on. Fixing
, suppose we have the following code:
x = 1:n;
non_adj = zeros(n,n-2);
for k=1:n
non_adj(k,:) = []; %in here comes the confusion
end
In non_adj(k,:) I would like to fill in x without the elements k-1 and k+1. However, these elements are regarded circularily, i.e. if I have
, then I want
(order of elements irrelevant). I know I can do this with if statements handling the
cases, but I was wondering if you all had a nicer way to one liner the construction.
I attempted the following construction
non_adj(k,:) = [k, k+2:mod(k-3,n)+1, mod(k+1,n)+1:k-2];
However this fails, for example, where
. The above would construct the array
, where in fact I wanted
.
Do you have any ideas? If it helps, the context is that I'm trying to construct a vertex set minus the neighbors of vertex k, which happen to be
(circularily shifted for
).
EDIT: I've realized I could use set differencing, i.e.: (however, you still might come up with better ideas).
non_adj(k,:) = setdiff([1:n], 1 + [mod(k-2,n) mod(k,n)]);
2 个评论
madhan ravi
2019-4-7
explicitly state your desired output
Abhijit Chowdhary
2019-4-7
编辑:Abhijit Chowdhary
2019-4-7
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!