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