how to split column vector when 0 come?

1 回表示 (過去 30 日間)
Sagar Dhage
Sagar Dhage 2014 年 9 月 10 日
編集済み: Robert Cumming 2014 年 9 月 10 日
I have a column vector A= (0;23;45;64;34;3;0; 12; 14;0; 12;354;13;324;25;254;0) I want to split A column vector when ) values 0 come, such that i should get A1= (0;23;45;64;34;3;0) A2= (0; 12; 14;0) A3=(0; 12;354;13;324;25;254;0)

回答 (1 件)

Robert Cumming
Robert Cumming 2014 年 9 月 10 日
編集済み: Robert Cumming 2014 年 9 月 10 日
You could do:
A= [0;23;45;64;34;3;0; 12; 14;0; 12;354;13;324;25;254;0];
index = find(A==0);
for ii=1:length(index)-1
data.(sprintf('A%i',ii)) = A(index(ii):index(ii+1));
end
Note: I have saved the data in fields in a struct called data - so you get:
data.A1
data.A2
data.A3
edit added link below
This is a better way that using eval to create A1, A2 or A3 for example. See this question and answer for an excellent explanation on why...
  1 件のコメント
Sagar Dhage
Sagar Dhage 2014 年 9 月 10 日
Thanks Robert. I would be great if we can save A1, A2, A3 in separate column.

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

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by