Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

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


Math Forum » Discussions » Software » comp.soft-sys.matlab

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

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
dpb

Posts: 7,876
Registered: 6/7/07
Re: Combine string matrix and integer matrix
Posted: Aug 24, 2013 12:49 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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 {}
curly braces instead of parens...

>> 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
>>

--




Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.