Trying to randomly permute a time series, error with time point

Dear Matlab,
I have an EEG signal, which I ran through wavelet convolution to produce a 3D matrix of time-frequency values, that is:
1 X 68(frex) X 4000(time points)
GOAL: Now the next step is to randomly slice the tf matrix and reoder time zero (and effectively reorder the map, to 'shuffle' it), by running the tf map/variable through 1000 rounds of permutations.
(see code below here, and attached tf mat file to run it):
% PERMING power values
num_iterations = 1000;
permuted_results = zeros(num_iterations,length(frex),length(times2));%zeros(1,num_iterations);
for ni=1:num_iterations
% randomly resort power values
cutpoint = times2(randi(numel(times2)));
permuted_results(ni,:,:,:) = tf(:,:,[ cutpoint:end 1:cutpoint-1 ]);
end
PROBLEM: when I run the for loop, I get this error:
Index in position 3 is invalid. Array indices must be positive integers or logical values.
Is it because the time series, which goes from -1000 to 3000 milliseconds, is it the negative values in there?
Thanks very much in advance for any help/guidance!
Best,
Joanne

2 Comments

"Is it because the time series, which goes from -1000 to 3000 milliseconds, is it the negative values in there?"
Yes.
load('TFsample.mat')% PERMING power values
num_iterations = 1000;
permuted_results = zeros(num_iterations,length(frex),length(times2));%zeros(1,num_iterations);
for ni=1:num_iterations
% randomly resort power values
cutpoint = times2(randi(numel(times2)))
permuted_results(ni,:,:,:) = tf(:,:,[ cutpoint:end 1:cutpoint-1 ]);
end
cutpoint = 2956
cutpoint = 119
cutpoint = -130
Index in position 3 is invalid. Array indices must be positive integers or logical values.

Sign in to comment.

 Accepted Answer

A=[1 2 3 4 5 6 7]
A = 1×7
1 2 3 4 5 6 7
A(1:3)
ans = 1×3
1 2 3
A(-1:2)
Array indices must be positive integers or logical values.
Yes, Matlab needs the index of the position starting from 1, so if you use negative numbers there will be an error.

6 Comments

well, not using negative values, say you were trying
A(-1000:4000)
you can try
A(1001+(-1000:4000))
so that there are no negative numbers. Remember that whatever is inside the brackets is AN INDEX to a position in a matrix and not a value or related to a value.
Hi Constantino,
Thanks so much for your help! I tried the code and variations pasted below, but it didn't work.
Did not work below (as after a few runs, it still randomly picks a negative number):
cutpoint = times2(randi(numel(times2(1002:4000))));
test = 1001+(-1000:3000);
test(4001) = [];
cutpoint = times2(randi(numel(times2(test))));
If you happen to come across a solution to make the loop run, regardless of whether the number is positive or negative, please let me know.
Thanks so much again,
Joanne
The problem is that you are confusing values of your arrays, with the indices of your arrays. If I understand correctly, cutpoint is an index of your array and you want to shuffle your array based on that location (not value) of your array, and you want to select that randomly.
cutpoint = times2(randi(numel(times2(1002:4000))));
This line will find the number of elements of times2 (which you already have as numel(times2(1002:4000)) is the same as numel(1002:4000)) then you generate an array of random numbers from 1 to the number of your elements, So far so good your number will be the index and will always be positive. THEN you use that to find the VALUE of times 2 at that index. That can be anything depending on what your values of times2 are, it could be an imaginary or negative number. Try the following:
% randomly resort power values
cutpoint = (randi(numel(times2)));
permuted_results(ni,:,:,:) = tf(:,:,[ cutpoint:end 1:cutpoint-1 ]);
"How to fix?"
It depends on what you want to do.
You can either use abs()
cutpoint = abs(times2(randi(numel(times2))))
or add 1001 to times2 (as suggested above) to adjust its values, which will change to 1 to 4001 (milliseconds).
times2 = times2 + 10001;
cutpoint = times(randi(numel(times2)))
%similar to the above two lines
cutpoint = randi(numel(times2))
Dyuman, I tried abs and it worked!! Thank you!!!
(and thank you also Constantino!)

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!