How to join two numeric arrays into a single string?

Hi guys, I have a situation that: I have one vector A, say 10000x1, and another vector B 10000x1, both are numerical arrays with floating point numbers in it. Now I want to write the data into one line of string as below:
A(1):B(1) A(2):B(2) ....A(10000):B(10000)
Is there an efficient way to do this? Right now, i am just using a for loop, change the floating number to string first, than add the ':', and then concatenate them together. This is very slow. Could anybody help? Thanks a lot.

 Réponse acceptée

A=rand(10,1);
B=rand(10,1);
out=sprintf('%f:%f ',A,B)

4 commentaires

Hi Azzi, thanks for your quick reply, it works like charm. But if A are all integers (from 1 to N), how to do the same thing? I use out=sprintf('%d:%f ',A,B)
The output is: 1:2.00000 3:4.00000 .....
It seems that it only print A, there is no B show up. thanks.
Stephen23
Stephen23 le 20 Mai 2016
Modifié(e) : Stephen23 le 20 Mai 2016
@Yi Zhu: if it doesn't give the correct result, then why did you accept this answer?
See my answer for the correct solution.
@stephen: This solution is correct to my initial problem, when A and B are both double vector, so I accept it.But soon I had trouble when A is integer vector, so I asked again. Thanks for your time and efforts, I appreciate it.
Stephen23
Stephen23 le 20 Mai 2016
Modifié(e) : Stephen23 le 20 Mai 2016
@Yi Zhu: this doesn't give the correct solution according to your original question, because the order is wrong This answer actually gives an output in order of A(1):A(2) A(3):A(4)... B(1):B(2) B(3):B(4)... which is definitely not the order that you specified in your question: A(1):B(1) A(2):B(2).... You just didn't check it properly:
>> A = [0;1;2;3;4];
>> B = [5;6;7;8;9];
>> out = sprintf('%d:%d ',A,B)
out = 0:1 2:3 4:5 6:7 8:9
As I wrote in my answer, the correct solution (to match the order in your question) is to concatenate the two vectors beforehand.

Connectez-vous pour commenter.

Plus de réponses (2)

I don't know if its faster but you can try
str = [sprintf('%f:',A) sprintf('%f:',B)];
str(end) = [];

1 commentaire

Hi goerk, thanks for your quick reply, your solution also works. But same problem, if A are all integers (from 1 to N), how to do the same thing? I use out=sprintf('%d:%f ',A,B)
The output is: 1:2.00000 3:4.00000 .....
It seems that it only print A, there is no B show up. thanks.

Connectez-vous pour commenter.

Stephen23
Stephen23 le 20 Mai 2016
Modifié(e) : Stephen23 le 20 Mai 2016
The correct solution is to first concatenate the matrices (and because you have column matrices you also need to transpose):
>> A = [0;1;2;3;4];
>> B = [5;6;7;8;9];
>> sprintf('%d:%d ',[A,B].')
ans = 0:5 1:6 2:7 3:8 4:9
Note that evaluating this string to generate a numeric vector will be very inefficient:
and a much better solution is to use arrayfun:
>> C = arrayfun(@(a,b)a:b,A,B,'Uni',false);
>> [C{:}]
ans =
0 1 2 3 4 5 1 2 3 4 5 6 2 3 4 5 6 7 3 4 5 6 7 8 4 5 6 7 8 9

3 commentaires

Yi Zhu
Yi Zhu le 20 Mai 2016
Modifié(e) : Yi Zhu le 20 Mai 2016
Thanks for the detailed answer. But still, when A is integer vector, B is double vector,
A = 1:10
B = randn(10,1)
sprintf('%d:%d ',[A,B].')
The result is
1:2 3:4 5:6 7:8 9:10 6.088942e-01:8.072232e-01 4.285545e-01:8.931010e-01 4.249390e-01:4.236594e-01 6.304087e-01:1.888050e-01 4.633747e-01:9.932405e-01
It is not what I want, I want the integer and double to interleave together. Could you show me how to do this? Thanks a lot.
BTW, I want the double to be shown in double format, not in the scientific 'e-' form.
Stephen23
Stephen23 le 20 Mai 2016
Modifié(e) : Azzi Abdelmalek le 20 Mai 2016
>> A = (1:10).';
>> B = randn(10,1);
>> sprintf('%d:%f ',[A,B].')
ans = 1:-0.356415 2:0.039756 3:-2.294004 4:0.555082 5:-1.860219 6:1.430920 7:-0.423892 8:-1.010463 9:0.515073 10:-0.2211
28
And read the fprintf documentation for more information on defining the format string.
Thanks @Stephen, it works. Thank you very much.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Characters and Strings dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by