An easy Array question

1 回表示 (過去 30 日間)
Hello kity
Hello kity 2012 年 12 月 12 日
I have a set of data seprated by Nans, example; DATA= [1; 2; 3; 4; NaN ; 6 ;7 ; NaN]
I want to do a calculation in every set of rows (example 1- 4 and 6-7).
So I programmed and found all the begin and end indices of each row (in this example, begin [ 1 ; 4] and end [6 ; 7]).
so this is what i want: Have two columns with begin and end indices;
column 1; A=[1 ; 6 ] %%%these are the begin indices
column 2; B[ 4 ;7 ] %%these are the end indices
now i want created a new array (call it new array) that has counted 1-4 and 6-7.
Then all i need to do is out=data(newarray) and i ll have my rows which i use for calc

回答 (2 件)

Sean de Wolski
Sean de Wolski 2012 年 12 月 12 日
Are you sure you need a cell array?
I would just use a for-loop and loop over the start and end indices:
sidx = start index
eidx = end index
for ii = 1:numel(eidx)
do_something_with(X(sidx(ii):eidx(ii)));
end
Of course this is also how I would build the cell array (i.e. replace
do_something_with with C(idx) = {}
And preallocate C with
C = cell(numel(sidx,1));
  1 件のコメント
Hello kity
Hello kity 2012 年 12 月 12 日
i changed my question a bit, maybe it is more clear now.

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


Andrei Bobrov
Andrei Bobrov 2012 年 12 月 13 日
編集済み: Andrei Bobrov 2012 年 12 月 13 日
DATA= [1; 2; 3; 4; NaN ; 6 ;7 ; NaN];
a = [1;4];
b = [6;7];
d = DATA(~isnan(DATA));
out = mat2cell(d(:),[diff(a)+1,diff(b)+1],1);
  2 件のコメント
Jan
Jan 2012 年 12 月 13 日
I think there must be a ~ before isnan.
Andrei Bobrov
Andrei Bobrov 2012 年 12 月 13 日
編集済み: Andrei Bobrov 2012 年 12 月 13 日
Hi Jan, thank you. Corrected.

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

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by