Matrix column selection based on vector with indices
1 view (last 30 days)
Show older comments
Hi all. I have a matrix of size m x n, with all entries as one at initialization. I also have a vector of size 1 x m. Each element is supposed to be between 1 and n, and the corresponding matrix entries are set to 0 (i.e. in the m-th matrix row, this is the column given by the m-th element of the vector). The code for that:
mask(sub2ind(size(mask),(1:numel(vector.'))',vector.')) = 0;
However, due to some modifications I had to make to the code, some elements of the vector can now take a value of 0, i.e. an invalid index. I would simply like to skip those elements. How can I do that?
0 Comments
Accepted Answer
More Answers (1)
David Hill
on 10 Oct 2022
Why not just a simple loop?
k=ones(10,14);
v=randi(size(k,2),1,size(k,1));
for j=1:size(k,1)
k(j,v(j))=0;
end
See Also
Categories
Find more on Polynomials in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!