reshaping a vector of dates (2)

1 回表示 (過去 30 日間)
antonet
antonet 2012 年 7 月 20 日
Dear all, I have a cell column vector that contains the following elements
AAA={'AUGUST-SEPTEMBER 1999' ...
'OCTOBER-NOVEMBER 1999'...
'DECEMBER-JANUARY 2000'...
'FEBRUARY-MARCH 2000'...
'APRIL-MAY 2000'...
'JUNE-JULY 2000'}
I want to change that vector so as to have
AAA={'AS 1999'...
'ON 1999'...
'DJ 2000'...
'FM 2000'...
'AM 2000'...
'JJ 2000'}
Where AS for example stands for 'AUGUST-SEPTEMBER’
thanks
[EDITED, code formatted, Jan]
  2 件のコメント
Jan
Jan 2012 年 7 月 20 日
編集済み: Jan 2012 年 7 月 20 日
@antonet: Please learn how to format your code properly. The more readable the question is, the easier is it to answer. This is your 50th question and I asked you repeatedly to care for a proper formatting.
antonet
antonet 2012 年 7 月 20 日
thanks simon. Sorry for this.

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

採用された回答

Andrei Bobrov
Andrei Bobrov 2012 年 7 月 20 日
編集済み: Andrei Bobrov 2012 年 7 月 20 日
g = regexp(AAA,'(^\w)|(-\w)|( \d*)','match');
AAA = strrep(cellstr(cell2mat(cat(1,g{:}))),'-','');
EDIT
  3 件のコメント
antonet
antonet 2012 年 7 月 20 日
Hi Andrei. I get the following error mesage
??? Index exceeds matrix dimensions.
Error in ==> @(x)strcat(f1{x,1:2},',',f1{x,3:4})
thanks
Andrei Bobrov
Andrei Bobrov 2012 年 7 月 20 日
Thank you Jan for your comment.

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

その他の回答 (1 件)

Jan
Jan 2012 年 7 月 20 日
編集済み: Jan 2012 年 7 月 20 日
AAA={'AUGUST-SEPTEMBER 1999'; ...
'OCTOBER-NOVEMBER 1999'; ...
'DECEMBER-JANUARY 2000'; ...
'FEBRUARY-MARCH 2000'; ...
'APRIL-MAY 2000'; ...
'JUNE-JULY 2000'};
Replace = {'AUGUST-SEPTEMBER', 'AS'; ...
'OCTOBER-NOVEMBER', 'ON'; ...
'DECEMBER-JANUARY', 'DJ';, ...
'FEBRUARY-MARCH', 'FM'; ...
'APRIL-MAY', 'AM'; ...
'JUNE-JULY', 'JJ'};
for i = 1:size(Replace, 1)
AAA = strrep(AAA, Replace{i, 1}, Replace{i, 2});
end
[EDITED] faster:
...
for i = 1:size(Replace, 1)
key = Replace{i, 1};
match = strncmp(AAA, key, length(key));
AAA(match) = strrep(AAA(match), key, Replace{i, 2});
end
While the 1st method needs 3.93 seconds if AAA is a {1572864 x 1} cell string, the smarter 2nd methods needs 1.42 seconds.
  4 件のコメント
antonet
antonet 2012 年 7 月 20 日
thanks simon for your reply. The above AAA is a sample. My original vector is of size 300 by 1. So it will take a lot of time to construct the "Replace" cell. this is the only problem that I have and to be honest it is ok for me if the code is slow as long as I do not need to do something extra.
Thanks
Jan
Jan 2012 年 7 月 20 日
編集済み: Jan 2012 年 7 月 20 日
I still do not understand, why the construction of "Replace" should take a lot of time and why this is influenced by the size of AAA. A vector of length 300 cannot be called "huge".
It seems to me like you did not post the complete problem. Unfortunately your decision to omit obviously necessary details leads to the fact, that the creation of my answer has wasted my time - and your time also. Please post all relevant details in future questions, especially the sizes of the real data.

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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by