Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to edit cells array in matlab

Tags:

matlab

cells

I have this cells array which is came from a mat lab code that generates dewey IDs:

 POT1 = 

        'a0'      []      []      []
        'a0'    'c0'      []      []
        'a0'    'b0'      []      []
        'a0'    'c1'      []      []
        'a0'    'd0'      []      []
        'a0'    'c0'    'd1'      []
        'a0'    'b0'    'd2'      []
        'a0'    'd0'    'd3'      []
        'a0'    'd0'    'c2'      []
        'a0'    'd0'    'b1'      []
        'a0'    'd0'    'd4'      []
        'a0'    'c1'    'c3'      []
        'a0'    'c1'    'b2'      []
        'a0'    'c1'    'c3'    'd5'
        'a0'    'c1'    'b2'    'd6'
        'a0'    'd0'    'b1'    'd7'
        'a0'    'd0'    'c2'    'd8'

note that column 1 is parent of column 2 and column 2 is paret of column 3..etc

so I want to build a code that gives the full name of each cell as follow:

POT1 =

  a0      []       []          []
  a0    a0.c0      []          []
  a0    a0.b0      []          []
  a0    a0.c1      []          []
  a0    a0.d0      []          []
  a0    a0.c0    a0.c0.d1      []
  a0    a0.b0    a0.b0.d2      []
  a0    a0.d0    a0.d0.d3      []
  a0    a0.d0    a0.d0.c2      []
  a0    a0.d0    a0.d0.b1      []
  .
  .
  .
  .     

The code which I build is not complete and gives me :" Index exceeds matrix dimensions" error :

for i=1:length(POT1)
      for j=3:size(POT1,2)
          if ~isempty(POT1{i,j})
          POT1{i,j}=[POT1{i,j-2} POT1{i,j-1} POT1{i,j}];
          end
      end
  end
  POT1
like image 244
Gloria Avatar asked Jan 19 '26 02:01

Gloria


2 Answers

I think you're on the right track, but it's easier if you process it column by column. This way, you just have to look one column back for each entry:

for jj=2:size(POT1,2)
    for ii=1:size(POT1,1)
        if ~isempty(POT1{ii,jj})
            POT1{ii,jj}=[POT1{ii,jj-1} '.' POT1{ii,jj}];
        end
    end
end

btw: length returns the Length of vector or largest array dimension, so next time, better use size.

like image 170
Gunther Struyf Avatar answered Jan 21 '26 22:01

Gunther Struyf


If you have vectors with ' %Two spaces ' on the empty spaces it will be really easy.

You can just transform it into a matrix and the rest is simple as this:

[POT1(:,1:2) '.' POT1(:,3:4)] 

Afterwards you can just strip the spaces and done.

like image 30
Dennis Jaheruddin Avatar answered Jan 21 '26 21:01

Dennis Jaheruddin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!