Merging ascii and number matrices

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Merging ascii and number matrices

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

etc.

I thought i could get this by using fprintf, but i could not figure out.

Thank you in advance,
Cheers.
_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Merging ascii and number matrices

NJank
On Wed, Mar 1, 2017 at 12:16 PM, Ozan Dernek <[hidden email]> 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.


_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Loading...