how to horizontally concatenate two strings, being one a cell array and the other a num2str converted gpuarray?
Show older comments
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
Accepted Answer
More Answers (2)
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 Comments
Isabel
on 21 Aug 2017
Adam
on 21 Aug 2017
I hadn't even noticed the gpuArray, to be honest. Why are you using a gpuArray to do such a short calculation?
Isabel
on 21 Aug 2017
José-Luis
on 22 Aug 2017
How do you have your data? Is it a text file?
If performance is that big of an issue, why use Matlab at all?
It's pretty, but fast it might not be sometimes.
Adam
on 22 Aug 2017
doc profile
will also give you a good idea as to where the bottlenecks are. Code that takes 15 days to run is either very very sub-optimal or must be doing something spectacular or working on a truly vast amount of data!
Isabel
on 22 Aug 2017
Isabel
on 22 Aug 2017
Adam
on 22 Aug 2017
Speed can usually be improved a lot in Matlab, especially if you write your initial code with no particular care to being optimal (which I have done many times - get the code to do what you want first, then worry about speed!). It takes time and knowledge though and is always a trade-off between time spent optimising and time spent just letting the code run.
I have done plenty of optimisation work on algorithms of mine of late and have sometimes achieved speedups of x1000 by changing some seriously slow bits of code or redesigning how I store the data or access it etc. The profile is excellent at finding bottleneck points, but you need to ally it with knowledge of Matlab to come up with alternative ideas for these (or ask, of course). Generally nowadays I tend to have numerous ideas for alternative approaches to things, though sometimes they are all slower.
I also bought Yair Altman's book on Accelerating performance in Matlab though I am only ~100 pages into its > 700 pages so far!
José-Luis
on 22 Aug 2017
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
on 22 Aug 2017
Andrei Bobrov
on 21 Aug 2017
C = { 'CELL:','CELLA:','CHAR:','GCH4:','GH2:' }
S = string(C) + [.1:.1:.5;.1:.2:.9]
out = join(S,',');
2 Comments
Walter Roberson
on 21 Aug 2017
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
on 22 Aug 2017
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!