Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Combine string matrix and integer matrix
Replies: 7   Last Post: Aug 25, 2013 9:11 AM

 Messages: [ Previous | Next ]
 dpb Posts: 9,850 Registered: 6/7/07
Re: Combine string matrix and integer matrix
Posted: Aug 24, 2013 12:49 AM

On 8/23/2013 10:45 PM, Sincloe Brans wrote:
> dpb <none@non.net> wrote in message <kv8s2n\$a81\$1@speranza.aioe.org>...
...

>> You _can't_ concatenate different types into the same array.
>>
>> You _can_ make C a cell array w/ a numeric and a character array in
>> the cells.
>>
>> Or, you can convert all the numeric values to strings and create a
>> larger character array--a cell array would probably be more handy.
>>
>> What is the end result of this desired operation, pray tell? If you
>> were describe what your actual objective is in the end instead of how
>> to do what is essentially impossible a better solution may ensue.
>>

...

> My end result should be e.g.
> C= [A B] = 1 3 4 4 a b c
> 3 3 4 5 a a a
> 4 4 5 5 a c a
> 5 4 4 5 a b c
>
> Converting all to any one type is totally fine.
> I used 'cellstr' to convert the B matrix and then I combined using cell
> array,
> C = [num2cell(A) cellstr (B)]
>
> when I do this way i see the strings with ' ' in each cell which is hard
> if I want to save in excel and do some calculations.
> My final matrix should have no 'a', just a (as shown in C matrix). Not
> sure if it is possible in matlab, if not I can live with this.
> Thank you so much for your time and helping!

>> B = {'a b c';
'a a a';
'a c a';
'a b c' };
>> A = [1 3 4 4; 3 3 4 5; 4 4 5 5; 5 4 4 5];
>> c=[num2str(A) repmat(' ',size(A,1),1) char(B)]

c =
1 3 4 4 a b c
3 3 4 5 a a a
4 4 5 5 a c a
5 4 4 5 a b c
>>

Looks good, doesn't it? Looks can be deceiving on the other end, however...

NB

>> c=[num2str(A) repmat(' ',size(A,1),1) char(B)]
c =
1 3 4 4 a b c
3 3 4 5 a a a
4 4 5 5 a c a
5 4 4 5 a b c
>> c(1)
ans =
1

Returns only the first character in the character array--to get the
string you would have to use the full subscripting--

>> c(1,:)
ans =
1 3 4 4 a b c
>>

OTOH, if you make a cell array,

>> c={[num2str(A) repmat(' ',size(A,1),1) char(B)]}
c =
[4x16 char]
>> c(1)
ans =
[4x16 char]

Not what you wanted, you have to dereference the cell content w/ the {}

>> c{:}
ans =
1 3 4 4 a b c
3 3 4 5 a a a
4 4 5 5 a c a
5 4 4 5 a b c
>> c{1}
ans =
1 3 4 4 a b c
3 3 4 5 a a a
4 4 5 5 a c a
5 4 4 5 a b c
>>

--

Date Subject Author
8/23/13 John Dickson
8/23/13 Saravanan Mani
8/23/13 John Dickson
8/23/13 dpb
8/23/13 John Dickson
8/24/13 dpb
8/25/13 John Dickson
8/25/13 dpb