0

I would like to apply an external code with a particualr function (chaos; https://figshare.com/s/80891dfb34c6ee9c8b34) from a paper by Toker et al. 2020 (https://www.nature.com/articles/s42003-019-0715-9#Sec1) in order to test whether my data are stochastic or chaotic.

I tried loops, for, cellfun, but none of these work in this case.

When I tried to use the following code:

T = readtable('ibfrq3.csv');
C = table2array(T);
D = num2cell(C, 2);
rowSums = cellfun(@sum, D)
chaos = cellfun(@chaos, D)

I received three errors:

Unable to perform assignment because the size of the left side is 1-by-14 and the size of the right side is
1-by-3.

Error in chaos>surrogate (line 989)
        surr(k,:)=unwrap(horzcat(st,parts{randperm(j)},en));

Error in chaos (line 157)
            [surr, params] = surrogate(zscore(surr_y), num_surr, 'CPP', 1, 1); 

Interestingly, I am able to make the function work, but only for separate rows (by copying them into square brackets and labelling as y e.g. y = [1,2,3,4,5,6,7,8,9,5,6,7,8,8]).

My desired output is a string (or ideally a column added to my .csv) with chaos function outputs (one word per row: 'stochastic' or 'chaotic' depending on the result).

My data are available here: https://drive.google.com/file/d/1I2BChrv0iqNr1dcEKTQKxKF7DDl_hF23/view?usp=sharing The .csv consists of allele frequencies over different time periods.

EDIT: Trying

numRows = size(C, 1);
for row = 1:numRows
    result(row) = chaos(C(row,:));
end

Yielded similar errors as above.

I also tried to run the code row by row on my data to check which row causes problems using

row1 = C(1,:);
chaos(row1)

for every row. Interestingly, it works perfectly giving the desired output for rows no.1, 3-9, 11-100. Rows no.2 and no.10 are not different from the others and do not contain any special characters or values. I have no clue what's wrong with them.

1 Answers1

0

cellfun (and its close relative arrayfun) are not suitable here because they apply a function to each element of an array separately, while you want to apply a function to each row. Also, you're assigning a value to chaos which is already the name of a function, which is likely to lead to confusing behaviour.

If C is an m x n numeric array, and chaos is a function that takes a row vector as input and returns a scalar value as output, then

numRows = size(C, 1);
for row = 1:numRows
    result(row) = chaos(C(row,:));
end

will create a 1 x m vector result containing the output of the function for each row of C.

If the chaos function returns a character vector, you'll need to store the result in a cell array instead, by changing the line inside the loop to

result{row} = chaos(C(row,:));

However the error message you've added to your question suggests there's another issue, because it tells us that an operation inside the chaos function is failing. If you say that chaos seems to work OK when you give it one row of data at a time, then either your array C doesn't contain what you think it does, or there's a row of data that causes it to fail for some reason. You need to debug this. For a start you could add a disp(row) inside the loop, which will show you which row of C causes the error.

nekomatic
  • 5,988
  • 1
  • 20
  • 27