フィルターのクリア

Cell and String manipulation

1 回表示 (過去 30 日間)
susan
susan 2012 年 9 月 10 日
Hi, I have A= <900x1> cell The cell contents are as follows:
AA
BB
CC
AA
BB
CC
.
.
.
1) I need to separate the above into blocks of 300. i.e. change the first 300 into the following:
AA1
BB1
CC1
...
Then the next 300 to AA2 BB2 CC2
And the following 300 to
AA3
BB3
CC3
2)So now I have A = {AA1} {BB1} {CC1} {AA1} {BB1} {CC1}... {AA2} {BB2} {CC2} i.e. I have sets of AA*, BB*, CC* that repeat. The first 300 have a suffix of 1, and so on..
I have thus 300 CCs. I have another variable h = 300x1 which is made up of ones and zeros.
h = [ 1 0 1 ...]
i.e. every CC in A has a corresponding h
I need to do the following
if h = 1, then the set (AA*,BB*,CC*) preceding the corresponding CC become AA1c, BB1c, CC1c.
if h = 0, then the set preceding the corresponding CC become AA1ic, BB1ic, CC1ic
So A = {AA1c} {BB1c} {CC1c} {AA1ic} {BB1ic} {CC1ic}... {AA2c} {BB2c} {CC2c}
I am not sure if I managed to convey the issue here so please let me know if I hav'nt and I will try to better explain.
Thank you.

採用された回答

Andrei Bobrov
Andrei Bobrov 2012 年 9 月 10 日
編集済み: Andrei Bobrov 2012 年 9 月 10 日
a0 = cellstr(('1':'3')');
a1 = {'';'i'};
[i1,i2]=ndgrid(1:3,h+1);
out = strcat(A,a0(i1(:)),a1(i2(:)),'c');
  1 件のコメント
susan
susan 2012 年 9 月 11 日
Wow! So elegant :)

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

その他の回答 (2 件)

susan
susan 2012 年 9 月 11 日
Actually I need a couple of changes.
A =
'AA' 'BB' 'CC' 'AA' 'BB' 'AA' 'BB' 'CC' 'AA' 'BB' 'CC' 'AA' 'BB' 'CC' 'AA'
%------
h =
1 0 1 1 0
%------
Right now out =
'AA1ic'
'BB2ic'
'CC3ic'
'AA1c'
'BB2c'
'AA3c'
'BB1ic'
'CC2ic'
'AA3ic'
'BB1ic'
'CC2ic'
'AA3ic'
'BB1c'
'CC2c'
'AA3c'
What I need is:
'AA1ic'
'BB1ic'
'CC1ic'
'AA1c'
'BB1c'
'AA1c'
'BB2ic'
'CC2ic'
'AA2ic'
'BB2ic'
'CC2ic'
'AA3ic'
'BB3c'
'CC3c'
'AA3c'
i.e. blocks of 1s, 2s and 3s NOT blocks of 1,2,3...
Thank you again!
  3 件のコメント
susan
susan 2012 年 9 月 11 日
Actually can't because I need to preserve the order of h.
John Petersen
John Petersen 2012 年 11 月 21 日
編集済み: John Petersen 2012 年 11 月 30 日
the value of h is already embedded in the AA's, BB's, etc. so why do you need h anymore?

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


susan
susan 2012 年 9 月 11 日
I am probably being very daft here but need to figure out how to translate the answer for my actual data..
A=<1152x1> cell h=288 x 1
A = 'TSTR' 'Co02' 'RT ' 'TEND' 'TSTR' 'Co19' 'RT ' 'TEND'...
I basically need the first 384 values with the suffix 1, then the next 384 trials with suffix 2, and the last with 3.
Each trial has a single h value associated with it, so I have 288 (1152/4) of those..
Sorry for not getting this..

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by