From: Vinod Karanth on
Hi All;

I am trying to combine a matrix of numbers with a column vector of strings. I am looking at ways of combining these two into a single matrix.

Example:

% Matrix of numbers.
num_mat=[1,2,10;25,2,1;10,9,21];

% Coulmn vector of string, X.
for i = 1:size(num_mat,1)
str_vect(i,1)='X';
end

I know I can use this below--
% Required matrix.
Req_mat=[num2str(num_mat),str_vect]

But, the problem with the above matrix is that there is no space between the last column of num_mat and the string vector. I need to have atleast one space between them.

I really appreciate if anybody knows a way to solve this....

Thanks.
From: Nathan on
On Sep 8, 7:50 pm, "Vinod Karanth" <vinodkara...(a)gmail.com> wrote:
> Hi All;
>
> I am trying to combine a matrix of numbers with a column vector of strings. I am looking at ways of combining these two into a single matrix.
>
> Example:
>
> % Matrix of numbers.
> num_mat=[1,2,10;25,2,1;10,9,21];
>
> % Coulmn vector of string, X.
> for i = 1:size(num_mat,1)
> str_vect(i,1)='X';
> end
>
> I know I can use this below--
> % Required matrix.
> Req_mat=[num2str(num_mat),str_vect]
>
> But, the problem with the above matrix is that there is no space between the last column of num_mat and the string vector. I need to have atleast one space between them.
>
> I really appreciate if anybody knows a way to solve this....
>
> Thanks.

Req_mat=[num2str(num_mat),' ',str_vect];

There's your space.

Have you read about cell arrays?

Those might be of use to you as well.
-Nathan
From: Vinod Karanth on
Req_mat=[num2str(num_mat),str_vect]

Req_mat=[num2str(num_mat),' ',str_vect];

Hi Nathan,

Thanks for your reply. The output from Req_mat=[num2str(num_mat),str_vect] and Req_mat=[num2str(num_mat),' ',str_vect] are the same :-(....is there any other way out?

Thanks.
From: Nathan on
On Sep 8, 8:52 pm, "Vinod Karanth" <vinodkara...(a)gmail.com> wrote:
>  Req_mat=[num2str(num_mat),str_vect]
>
> Req_mat=[num2str(num_mat),' ',str_vect];
>
> Hi Nathan,
>
> Thanks for your reply. The output from Req_mat=[num2str(num_mat),str_vect] and Req_mat=[num2str(num_mat),' ',str_vect] are the same :-(....is there any other way out?
>
> Thanks.

Well, if you're making the whole thing a string anyways, why not use
sprintf?
Req_mat = sprintf(%s %s,num2str(num_mat),str_vect);

-Nathan
From: Vinod Karanth on

> Well, if you're making the whole thing a string anyways, why not use
> sprintf?
> Req_mat = sprintf(%s %s,num2str(num_mat),str_vect);
>
> -Nathan

Hi Nathan,

I checked with your above suggestion, but it just puts all the elements one next to another....

>> Req_mat = sprintf('%s %s',num2str(num_mat),str_vect)
Req_mat =
111111111122222123456789012345678901234
Please note that my num_mat has different values from what I showed in my first example. But the end result is not what I am after....Any other inputs please?

Thanks.