[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Combining cell (string) array elements
From: |
Philip Nienhuis |
Subject: |
Re: Combining cell (string) array elements |
Date: |
Tue, 7 Oct 2014 14:05:13 -0700 (PDT) |
Puffinman wrote
> I have two string arrays such as:
>
> {'a' 'b' 'c'}
> {'w' 'x' 'y' 'z'}
>
> and would like to combine them into a third 3x4 array with the elements:
>
> {'aw' 'ax' 'ay' 'az'
> 'bw' 'bx' 'by' 'bz'
> 'cw' 'cx' 'cy' 'cz'}
>
> Is there an elegant way I can do this without using ‘for’ loops?
Define "elegant".
Anyway, depending on the exact "orientation" (row, col vector) of a and b,
where
a = {'a' 'b' 'c'}
b = {'w' 'x' 'y' 'z'}
something like:
reshape (cellstr (cell2mat ([repmat(a, 4, 1)(:) repmat(b', 3, 1)])), 4, 3)'
should work without a for loop. Maybe you can simplify it a bit (I haven't
tried hard).
Note the lack of space between reshape and (...) between the brackets, and
also the transpose operators.
If you replace 3 and 4 by numel (a) and numel (b), resp., you can easily
generalize the solution.
Philip
--
View this message in context:
http://octave.1599824.n4.nabble.com/Combining-cell-string-array-elements-tp4666885p4666891.html
Sent from the Octave - General mailing list archive at Nabble.com.
- Re: Combining cell (string) array elements,
Philip Nienhuis <=