Removing elements of array by condition and without resizing?

I'm trying to produce an array to later write into a .txt file. Currently, the array I am working with is a 5323x18 double. Due to the nature of its conception, roughly all the first 3-5 columns contain a non-zero numerical value, while the other columns (6-18) occasionally contains a non-zero numerical value. The rest of the elements of the array are zero. In order to ensure that the .txt file doesn't have any of these zeros, is there any way I can delete those elements from the array, while maintaining the array size?
For example, turning something like the following array:
[ 1 5 0 0 0 0
1 5 6 7 0 0
4 0 0 0 0 0
5 5 5 5 5 5]
to
[ 1 5
1 5 6 7
4
5 5 5 5 5 5]

回答 (1 件)

madhan ravi
madhan ravi 2019 年 2 月 19 日

0 投票

Result=arrayfun(@(x)nonzeros(a(x,:)).',1:size(a,1),'un',0); % where a is your matrix
celldisp(Result)

5 件のコメント

Jonathan Cardoza
Jonathan Cardoza 2019 年 2 月 19 日
Is there a way to also separate the values in square brackets in the result cell that is formed?
madhan ravi
madhan ravi 2019 年 2 月 19 日
matrix cannot have holes so cell array is preferred
Jonathan Cardoza
Jonathan Cardoza 2019 年 2 月 19 日
And is it possible for the cell array to have holes?
madhan ravi
madhan ravi 2019 年 2 月 19 日
yes by holes I mean empty
Jonathan Cardoza
Jonathan Cardoza 2019 年 2 月 19 日
Ah, I see. I was hoping that the resultant combined row data in square brackets could be separated into their own cells.

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

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

タグ

質問済み:

2019 年 2 月 19 日

コメント済み:

2019 年 2 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by