-2

I have two cell array as follows

Z=  (2,5) (2,3) (2,1) (2,2) (2,4)
    (4,5) (4,3) (4,1) (4,2) (4,4)
    (5,5) (5,3) (5,1) (5,2) (5,4)
    (3,5) (3,3) (3,1) (3,2) (3,4)
    (1,5) (1,3) (1,1) (1,2) (1,4)



W=  'ATGC' 'ACTG' 'AGCT' 'AATT' 'TATG'
    'GCTA' 'GACT' 'TACG' 'TGAT' 'ATAT'
    'TCGG' 'CATA' 'TAAT' 'TTTT' 'CTGC'
    'AAGT' 'AAGG' 'TGGG' 'TGTA' 'CAAA'
    'GTGG' 'CTCT' 'GATT' 'CGTG' 'CTGG'

Now i have a big problem....i need to create a new cell array 'G' where the value of W should be placed accprding to the positions given in Z.

For eg: The value at (2,5) in W ie;ATAT should be placed at (1,1) of G Both the cell arrays are just examples...i need to do it for a 256*256 array. Please help.Thanks in advance

Divakar
  • 218,885
  • 19
  • 262
  • 358
  • 1
    I wished you could provide a clean example. This could not be executed in matlab. You can simply define 'Z' as a 2xn matrix and use it to index matrix G!! – Mohammad Aug 21 '14 at 04:31

2 Answers2

4

If we presume the example as:

Z = {[2,5] [2,3] [2,1] [2,2] [2,4]...
    [4,5] [4,3] [4,1] [4,2] [4,4]...
    [5,5] [5,3] [5,1] [5,2] [5,4]...
    [3,5] [3,3] [3,1] [3,2] [3,4]...
    [1,5] [1,3] [1,1] [1,2] [1,4]}

W = {'ATGC' 'ACTG' 'AGCT' 'AATT' 'TATG';...
    'GCTA' 'GACT' 'TACG' 'TGAT' 'ATAT';...
    'TCGG' 'CATA' 'TAAT' 'TTTT' 'CTGC';...
    'AAGT' 'AAGG' 'TGGG' 'TGTA' 'CAAA';...
    'GTGG' 'CTCT' 'GATT' 'CGTG' 'CTGG'}

Then we got:

G = cell(5,5)

I = [Z{:}]; %indices in a vector
x = I(2:2:end); y = I(1:2:end);
I = sub2ind(size(Z),y,x);
G(I) = W(1:numel(W));
Mohammad
  • 464
  • 1
  • 3
  • 17
  • 1
    Very good answer, but I personally would not use `i` as a variable as this is a reserved keyword for the complex number. Take a look at this post here: http://stackoverflow.com/questions/14790740/using-i-and-j-as-variables-in-matlab – rayryeng Aug 21 '14 at 05:15
  • 2
    Think you need this instead - `G(i) = W(1:numel(W))` – Divakar Aug 21 '14 at 05:31
2

Use indexing technique -

G = cell(size(W)) %// Create a empty cell array for storage of desired output
row_col = vertcat(Z{:}) %// Extract row and col indices from Z into numeric array
G(sub2ind(size(W),row_col(:,1),row_col(:,2))) = W %// Index G with row_col ...
                                                  %// and set values from W
Divakar
  • 218,885
  • 19
  • 262
  • 358