フィルターのクリア

how to horizontally concatenate two strings, being one a cell array and the other a num2str converted gpuarray?

1 回表示 (過去 30 日間)
Hello there, I am trying to concatenate two strings, but I'm missing something. The idea is to get these strings:
'CELL:0.1,CELLA:0.2,CHAR:0.3,GCH4:0.4,GH2:0.5'
'CELL:0.1,CELLA:0.3,CHAR:0.5,GCH4:0.7,GH2:0.9'
but my output is:
composition =
CELL:,CELLA:,CHAR:,GCH4:,GH2:0.1 0.2 0.3 0.4 0.5
composition =
CELL:,CELLA:,CHAR:,GCH4:,GH2:0.1 0.3 0.5 0.7 0.9
My code is below. Any help will be much appreciated. Thank you!
A1 = 0.1:0.1:0.5;
A2 = 0.1:0.2:1;
A1 = gpuArray(A1);
A2 = gpuArray(A2);
A = [A1;A2];
C = ['CELL:',',CELLA:',',CHAR:',',GCH4:',',GH2:'];
for i = 1:2
sp = sprintf('%s',C);
y_sp = sprintf('%s',num2str(A(i,:)));
composition = strcat(sp,y_sp);
display(composition);
end

採用された回答

Isabel
Isabel 2017 年 8 月 24 日
To whom it might concern I found the solution:
C = 'CELL:%.4f,CELLA:%.4f,CHAR:%.4f,GCH4:%.4f,GH2:%.4f';
for i = 1:2
composition{i} = sprintf(C,(A(i,1:a)));
composition{i} = num2str(composition{i});
end
  2 件のコメント
Stephen23
Stephen23 2017 年 8 月 24 日
編集済み: Stephen23 2017 年 8 月 24 日
Note that num2str does absolutely nothing here, and that line can be removed entirely from your code.
Stephen23
Stephen23 2017 年 8 月 24 日
編集済み: Stephen23 2017 年 8 月 24 日
You could very easily make your code adjust automatically for different sizes of A and C, and to automatically generate the sprint format string as well:
A = [0.1:0.1:0.5;0.1:0.2:1]
C = {'CELL','CELLA','CHAR','GCH4','GH2'};
%
fmt = sprintf(',%s:%%.4f',C{:});
fmt = fmt(2:end);
N = size(A,1);
Z = cell(N,1);
for k = 1:N
Z{k} = sprintf(fmt,A(k,:));
end
Note how I preallocated the output cell array, which for a small array like this might not be strictly necessary, but is a good habit to learn.

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

その他の回答 (2 件)

Adam
Adam 2017 年 8 月 21 日
編集済み: Adam 2017 年 8 月 21 日
If you define C as a cell array instead then, for example, this should work:
C = { 'CELL:',',CELLA:',',CHAR:',',GCH4:',',GH2:' }
cell2mat( reshape( [C; arrayfun( @num2str, A1, 'UniformOutput', false )], [1 10] ) )
In a char array all your components just get merged into one long char that is much more complicated to extract the components from to concatenate each one with an element of A1
  11 件のコメント
José-Luis
José-Luis 2017 年 8 月 22 日
That is fair enough. I like Matlab myself: Wouldn't be answering here otherwise.
If you want down to the metal performance, then it might not be the best choice. Because of this, C++ is my language of choice these days when I ---really--- need fast. Cost is also a problem: since University days, I haven't had an employer willing to shell out for all the toolboxes it'd be neat to have.
That's why, based on the kind of problem they have, I keep recommending people to look in other places as there are better (and possibly free) tools out there.
That being said, I do agree with you: slow code will be slow, be it in assembly or Matlab.
Isabel
Isabel 2017 年 8 月 22 日
@ Adam, Thank you very much, I will check the book as well :)

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


Andrei Bobrov
Andrei Bobrov 2017 年 8 月 21 日
C = { 'CELL:','CELLA:','CHAR:','GCH4:','GH2:' }
S = string(C) + [.1:.1:.5;.1:.2:.9]
out = join(S,',');
  2 件のコメント
Walter Roberson
Walter Roberson 2017 年 8 月 21 日
The above requires R2016b or later.
With R2017a or later, the above could also be coded as
C = [ "CELL:", "CELLA:", "CHAR:", "GCH4:','GH2:" ];
S = C + [.1:.1:.5;.1:.2:.9]
out = join(S,',');
Unfortunately both version of this will fail when the numeric array is a gpuArray :(
Isabel
Isabel 2017 年 8 月 22 日
Yes, it is true.. i tried it :\. Thank you very much for your answer

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

カテゴリ

Help Center および File ExchangeParallel Computing Fundamentals についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by