Changing the size of Vector (n no. of zeros to one zero and n no. of ones to 1)

1 回表示 (過去 30 日間)
ARN
ARN 2018 年 8 月 21 日
コメント済み: ARN 2018 年 9 月 12 日
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
  2 件のコメント
KALYAN ACHARJYA
KALYAN ACHARJYA 2018 年 8 月 21 日
Do you want the text to numeric?
ARN
ARN 2018 年 8 月 21 日
No A is vector and so is B. I just randomly made vector A, my original vector is different.

サインインしてコメントする。

採用された回答

Rik
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
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.
ARN
ARN 2018 年 9 月 12 日
Hi, Hope u can help again
A1= [- - - - - - - - - 15:30:00 15:30:01 15:30:02 15:30:03 15:30:04 15:30:05 15:30:06 15:30:07 - - - - 15:30:12 15:30:13 15:30:14 15:30:15 15:30:16 15:30:17 - - - - - - 15:30:24 15:30:25 15:30:26 15:30:27 - - 15:30:30 15:30:31 - - ];
How can i get B1 from A1 . A1 is random will change everytime
B1= [0 15:30:00 15:30:07 0 15:30:12 15:30:17 0 15:30:24 15:30:27 0 15:30:30 15:30:31 0];
Thanks, Link to the question: https://www.mathworks.com/matlabcentral/answers/418675-getting-the-indices-of-an-array-with-conditions

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by