How would I split a vector in two based on the data values of 1 or 0?
5 次查看(过去 30 天)
显示 更早的评论
Create two vectors from a long vector consiting of 1s and 0s. I have some data to sort and I have the values in binary form in a vector of a 43000x1 consisting of just 1s and 0s. I need to split this vector into two vectors, one consisting of 1s, and the other consisting of the 0s. This will result in two different sized vectors.
0 个评论
采纳的回答
Image Analyst
2023-1-23
编辑:Image Analyst
2023-1-23
vec = randi([0, 1], 43000, 1)
mask = vec == 1;
vec0 = vec(~mask);
vec1 = vec(mask);
whos vec0;
whos vec1
% Or another way
vec0 = zeros((length(vec) - nnz(vec)), 1);
vec1 = ones(nnz(vec), 1);
whos vec0
whos vec1
2 个评论
Image Analyst
2023-1-23
Not sure what you mean. A column is more than a single number.
Anyway, you can do masking on the first column and apply that to all columns:
m = randi([0, 1], 43000, 50);
% Make mask based on first column ONLY.
rowsWith1 = m(:, 1) == 1;
m0 = m(~rowsWith1, :);
m1 = m(rowsWith1, :);
whos m0;
whos m1
Note however that m0 and m1 will have a mixture of 0s and 1s in columns 2-50. Only the first column will be all 0 or all 1.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Web Services 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!