Remove zero padding in the vector which has element 0

I have vector A=[1 36 28 95 64 125 44 96 93 21 67 126 59 25 117 0 84 9 6] and then padded with zero until the length multiple of 3. So the newA=[1 36 28 95 64 125 44 96 93 21 67 126 59 25 117 0 84 9 6 0 0]. It is possible to remove zero padding? Because if i used nonzeros, the element 0 of A removed too. The number of elements A vector can not be ascertained

 採用された回答

Stephen23
Stephen23 2017 年 2 月 4 日

2 投票

You can use find for this:
>> newA = [1,36,28,95,64,125,44,96,93,21,67,126,59,25,117,0,84,9,6,0,0]
newA =
1 36 28 95 64 125 44 96 93 21 67 126 59 25 117 0 84 9 6 0 0
>> B = newA(1:find(newA,1,'last'))
B =
1 36 28 95 64 125 44 96 93 21 67 126 59 25 117 0 84 9 6

1 件のコメント

Tan
Tan 2017 年 2 月 5 日
編集済み: Tan 2017 年 2 月 5 日
It worked! Thank you Stephen for telling me:)

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

その他の回答 (1 件)

Star Strider
Star Strider 2017 年 2 月 4 日

1 投票

Using eps (or some other small number) to replace either your original zero or the padded zeros will work also, and not interfere with your calculations with them:
A=[1 36 28 95 64 125 44 96 93 21 67 126 59 25 117 0 84 9 6];
newA=[1 36 28 95 64 125 44 96 93 21 67 126 59 25 117 0 84 9 6 eps eps];
OriginalA = newA(A ~= eps)

2 件のコメント

Tan
Tan 2017 年 2 月 5 日
I just know there is a padding such as that. I've tried and it worked. Thank you Star Strider for telling me:)
Star Strider
Star Strider 2017 年 2 月 5 日
My pleasure!

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

カテゴリ

ヘルプ センター および File ExchangeOperators and Elementary Operations についてさらに検索

質問済み:

Tan
2017 年 2 月 4 日

コメント済み:

2017 年 2 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by