Changing the size of Vector (n no. of zeros to one zero and n no. of ones to 1)
1 次查看(过去 30 天)
显示 更早的评论
Hi all, I have a vector A and i need to convert it into B. How can i do that with coding? The last value is the only exception in terms of size;
zer= zeros(1,20);
on= ones(1,20);
A=[zer zer on on zer on zer on zeros(1,10)];
B=[0 0 1 1 0 1 0 1 0];
If found zeros put one zero in vector B and if ones then 1 in vector B
采纳的回答
Rik
2018-8-21
编辑:Rik
2018-8-21
This code should work to remove duplicate values.
A=[1 1 0 0 1 1 1 0 1 0 1];
%A=randi([0 1],1,100);
d=[false diff(A)==0];
A(d)=[];
old code:
If your input follows this pattern strictly, you can just use indexing to get the values:
zer= zeros(1,20);
on= ones(1,20);
A=[zer zer on on zer on zer on zeros(1,10)];
B=[0 0 1 1 0 1 0 1 0];
B2=A(1:20:end);
isequal(B,B2)
6 个评论
Rik
2018-8-21
Then B2=A(1:20:end); will work. It selects the first from every 20 elements in your vector, so it condenses your 1120 element vector down to 56 elements. Then call to end in that indexing takes care of any larger vector.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!