Remove all zeros from a vector (preallocated unused spaces)

1 回表示 (過去 30 日間)
fadams18
fadams18 2018 年 10 月 22 日
コメント済み: madhan ravi 2018 年 10 月 22 日
So i am saving frobenius norm of an algorithm. Before that i made my preallocation as
RRE=zeros(1,5000);
After saving my RRE values for total simulation of 20, I get an array of this nature
[0.9808 0.488 0.0819 0.11491 0 0 0 0 0 0 0 ] up to 5000
I want to plot only last values so I do this
Last_RRE= RRE(end);
So instead of returning 0.11491 it returns 0. How to i make sure these zeroes are removed. I have to do this for 5 other alogrithms. so i cant make a loop to go through to the length and if statement to remove zeroes. looks tiring for all these methods. since the RRE's are unique for each algorithm and have different names. NB, the zeroes are not zero value RRE they are just 'unused' or null. i dont know how to call them.

採用された回答

madhan ravi
madhan ravi 2018 年 10 月 22 日
RE = [0.9808 0.488 0.0819 0.11491 0 0 0 0 0 0 0 ]
nonzeros(RE) %removes zeros
  2 件のコメント
fadams18
fadams18 2018 年 10 月 22 日
Thanks this works well.
madhan ravi
madhan ravi 2018 年 10 月 22 日
Anytime :)

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

その他の回答 (1 件)

Stephan
Stephan 2018 年 10 月 22 日
編集済み: Stephan 2018 年 10 月 22 日
Hi,
build an example:
A=zeros(1,20)
A(1:10) = rand()
remove zeros:
A(A==0) = []
Best regards
Stephan
  1 件のコメント
fadams18
fadams18 2018 年 10 月 22 日
Thanks Stephan. For my work, nonzeros seems much simpler.

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

カテゴリ

Help Center および File ExchangePerformance and Memory についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by