Finding index in a set
2 次查看(过去 30 天)
显示 更早的评论
Hello. I have these two sets.
x1 = [0 , 0, 1, 0]
x2 = [0, 0, 0, 0]
I want to randomly generate one in x2 except the index 3, where the vaule is x1. The output will be look like:
x1 = [0, 0, 1, 0]
x2 = [1, 0, 0, 0] or [0, 1, 0, 0] or [0, 0, 0, 1].
How can I do that?
0 个评论
采纳的回答
Stephen23
2022-12-17
编辑:Stephen23
2022-12-17
Simple and efficient:
x1 = [0,0,1,0];
x2 = [0,0,0,0];
ix = find(~x1);
iy = randi(nnz(ix),1);
x2(ix(iy)) = 1
更多回答(1 个)
Arif Hoq
2022-12-17
编辑:Arif Hoq
2022-12-17
one approach:
x1 = [0 , 0, 1, 0];
x2 = [0, 0, 0, 0];
% [value I]=find(x1==1);
b=perms(x1);
[row,col,v] = find(b(:,3));
b(row,:)=[];
c=b;
output=unique(c,'rows','stable')
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!