With a large array A, and a small array B, how do I get a logical array with the size of A, where any element that is also contained in B is 1?
10 次查看(过去 30 天)
显示 更早的评论
Hello, and sorry if the title is unclear. I could not find a better way to word this.
I want to get all rows from a matrix, specified by a vector containing random integers that will be used as indices.
And I tried to do it with logical arrays, but I can't make it work.
So, I have a vector A, of variable size, that goes from 1 and keeps incrementing by 1 for every element. it is a row vector.
Then, I have another row vector B, it is smaller than A, but the few elements it has, is always contained in A too.
Last, I have a column vector C, which has the same length as A, and contains varying floats.
I am trying to find a way to create a logical column vector, where I transpose A, and every index that contained any of the elements from B is 1, and the rest is 0.
C = ones([10,1])
% A is a 1 x ? size vector, initialized with: 1:1:size(another_matrix,1)
% where another_matrix has variable size.
% B is a 1 x ? size vector, initialized with:
% randi([1,size(another_matrix,1),1,?]
% These two may or may not have the same size in runtime, but B will never
% be bigger than A.
C = C .* (A == B)
% I know that to remove the zeroes I need to do C(A == B), but I need to
% retain the same dimensions for later
Works when B was a scalar, and gave me the result I needed.
But when B becomes a vector, it gives me an error.
Using intersect or find did not make much sense, since every element in B is guaranteed to be contained in A.
Maybe the idea is already wrong. Or maybe I just do not know the correct way. Either way I need some help, and any answers will be greatly appreciated.
0 个评论
采纳的回答
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!