help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Merging ascii and number matrices


From: Nicholas Jankowski
Subject: Re: Merging ascii and number matrices
Date: Wed, 1 Mar 2017 13:33:53 -0500

On Wed, Mar 1, 2017 at 12:16 PM, Ozan Dernek <address@hidden> wrote:
Hi,

I am not experienced with octave, so i will appreciate if you could help. I have a column matrix containing letters and several column matrices containing non-integer numbers. For example:

A=[Aa;Bb;Cc] and B=[1.00;1.01;1.02], C=[2.00;2.01,2.02]…..Z=[26.00;26.01;26.02]

My purpose is to merge the matrix A into other matrices so that these letters will look like the labels of the numbers, such as:

B’=[Aa   1.00; Bb    1.01; Cc    1.02]
C’=[Aa   2.00; Bb    1.01; Cc    1.02]


A matrix cannot hold mixed data. You can convert the numbers to strings, then concatenate the letter and number strings using strcat() or just array concatenation syntax:

>> a = 1234
a =  1234

>> b = 'foo'
b = foo

>> a2 = num2str(a)
a2 = 1234

>> c = strcat(b,a2)
c = foo1234

>> c2 = [b,a2]
c2 = foo1234

>> c3 = [b," :  ",a2]
c2 = foo :  1234

a string is already a 1xn array.  so you can't have a 2d array of strings.  you can have a column array of strings. it will automatically pad unequal elements with whitespace as needed so it makes a uniform array.

>> [c;c2;c3]
ans =

foo1234
foo1234
foo :  1234

>> [c,c2,c3]
ans = foo1234foo1234foo :  1234

if you want a 2D array structure containing strings, you probably want to use a cell array. A cell can contain multiple data types:
>> {a,b;c,c2}
ans =
{
  [1,1] =  1234
  [2,1] = foo1234
  [1,2] = foo
  [2,2] = foo1234
}

So, you should be able to concatenate your numbers and strings, then assign them to a cell however you wish.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]