-2

hello I should translate the following matlab code in python, it use dynamic cell array:

NUM=zeros(size(CELLin));    NUM=NUM(:);
jALL=cell(0,0);
r=0;
for k=1:size(CELLin,1)
    r=r+1; NUM(r)=(numel(CELLin{k,1})-1)/2; % Ak NoptN
           jALL{r,1}=[k 1];
    r=r+1; NUM(r)=(numel(CELLin{k,2})-1)/2; % Ak NoptP
           jALL{r,1}=[k 2];
end

Thanks in advance for support.

Linux
  • 39
  • 7
  • Can you give a sample `CELLin` (and any other undefined variables) so I can at least run your code in an Octave session. It's hard to translate without a clear idea of what the code does. (Beware, people will complain that this is not a code translation service) – hpaulj May 02 '17 at 16:29
  • Ok, CELLin is a cell array with four entry (column), for each entry we have a vector of number, which that have to increase dynamically is the number of row of jALL cell array,CELLin is passed by input. Now I need to understand how to create in python a dynamic cell array, so that this can be useful also to others users. – Linux May 02 '17 at 17:22
  • The closest thing in Python to a cell is a list. – hpaulj May 02 '17 at 18:07
  • Technically you can't dynamically change the size of a matrix or cell array in MATLAB. MATLAB does a slow and expensive copy behind-the-scenes, which is why the MATLAB editor will warn you when it notices you doing it. – TheBlackCat May 02 '17 at 21:30

1 Answers1

0

If I define

CELLin = {[1,2],[1,2,3];[1,2,3],[1,2,3,4]}

and run your code in Octave I get

>> NUM
NUM =
   0.50000
   1.00000
   1.00000
   1.50000
>> jALL
jALL =
{
  [1,1] =
     1   1
  [2,1] =
     1   2
  [3,1] =
     2   1
  [4,1] =
     2   2

CELLin is a nx2 cell, and NUM gives some sort of mean length of the elements of the cell in a flatten layout (n*2 rows). jALL is just an indexing (possibly of the NUM).

Given the variable length of the elements of CELLin the simplest Python translation uses lists, not numpy. Lists also allow me to accumulate results without preallocation. For example the MATLAB code initials jALL as (0,0), but it grows with each r assignment.

# nested list; best equivalent to CELL
CELLin = [[[1,2],[1,2,3]],[[1,2,3],[1,2,3,4]]]

NUM = []
jALL = []
r = 0
for k, v in enumerate(CELLin): # iterate of 1st level
   r += 1
   NUM.append( (len(v[0])-1)/2.)
   jALL.append([r,1])
   r += 1
   NUM.append( (len(v[1])-1)/2.)
   jALL.append([r,2])
print(CELLin)
print(NUM)
print(jALL)

produces

1033:~/mypy$ python stack43742171.py 
[[[1, 2], [1, 2, 3]], [[1, 2, 3], [1, 2, 3, 4]]]
[0.5, 1.0, 1.0, 1.5]
[[1, 1], [2, 2], [3, 1], [4, 2]]

If I add

CELLin = np.array(CELLin)

it becomes a object dtype array (2d with list elements)

array([[[1, 2], [1, 2, 3]],
      [[1, 2, 3], [1, 2, 3, 4]]], dtype=object)

The rest of code runs the same

hpaulj
  • 221,503
  • 14
  • 230
  • 353
  • mmmm ok. But if i want create CELLin like a matrix rx4 where r change dynamically how can i do?? – Linux May 02 '17 at 17:58
  • numpy has object arrays, but for most purposes they are just glorified or debased lists. Your code references 2 columns of CELLin. I don't know what you mean by 'change dynamically'. Nothing in my code is tied to a predefined `CELLin` size. – hpaulj May 02 '17 at 18:05
  • In matlab: `CELLin=cell(0,0); CELLin{1,1}=2 ; CELLin{2,1}=3` i would like reiterate the same in python, without preallocate CELLin, so its row number should be change dynamically – Linux May 02 '17 at 18:30
  • In Python the only structure that lets you add elements by simply indexing them is a dictionary. You can grow a list with `append` or `extend` methods. You can cancatenate arrays to make new ones. I suspect MATLAB is doing the same under the covers (making a new matrix). – hpaulj May 02 '17 at 19:08