3D array, remove columns where second array == -1

1 view (last 30 days)
Hi,
I am curently processing 13 channels of PSG data. I take my data and create 10 second buffers with 5 second overlaps for the length of the data. This returns a 3D array "windows3D" which is 2000*5147*13 (buffer width * no. of buffers * channels)
I have a second array called "truthBuffer" which is 1*5147, this array tells me if the 2000 samples have been identified by a clinican as positive (1) negitive (0) or are not scored (-1). I wish to preform Machine learning on this so I wish to remove the array elements of truthBuffer which are -1 and then remove the corresponding colums from all channels of windows3D.
This should return scoredTurthBuffer as a 1*2538 array (as there are 2609 '-1' values - this part works) and assessedWindows3D as a 2000*2538*13 (where the same 2538 colums are retained for all 13 channels) this part doesn't work
This is what i,ve tried below, this works well for "scoredTruthBuffer" (1*5147 ==> 1*2538) but "assessedWindows3D" changes from 2000*5147*13 to 1*133819391 array which is not what I expect. I am expecing an array of 2000*2538*13 back.
scoredTruthBuffer = truthBuffer; % Create a mirrior of truthBuffer || creates sized array 1*5147 as expected
assessedWindows3D = windows3D; % Create a mirrior of windows3D || creates sized array 2000*5147*13 as expected
toRemove = truthBuffer == -1; % Find what elements to remove in turthBuffer || 2609 '-1' values (5147-2609 = 2538)
scoredTruthBuffer(toRemove) = []; % remove elements from truthBuffer || creates sized array 1*2538 as expected
assessedWindows3D(toRemove) = []; % remove elements from truthBuffer || creates sized array 1*133819391 UNEXPECTED, expecting 2000*2538*13
I have also tried to include a for loop to preform this task channel by channel but I get the following error;
% Sudo
% for i = 1:13
% for j = 1:length(truthBuffer)
% if truthBuffer == -1
% windows3D(:, j ,i) = [];
% else
%
% end
% end
% end
% Error given
=>> A null assignment can have only one non-colon index.
I know the error is generated by the inclusion of both 'j' and 'i' but I cannot come up with a easy way around this.
How can I return "assessedWindows3D" as a 2000*2538*13 to match the columns removed in scoredTruthBuffer?
Thanks in advance,
Christopher

Accepted Answer

Dave B
Dave B on 10 Nov 2021
Edited: Dave B on 10 Nov 2021
From what I can see here, you've got a logical vector called toRemove, and you want to remove columns where toRemove is true from your matrix. But you didn't specify that it was columns which you wanted removed when you tried to remove from assessedWindows3D.
Here's a simpified example:
a = [1 -1 2 -1];
toRemove = a == -1;
assessedWindows3D = rand(2, 4, 2); % smaller version of assessedWindows3D so we can see it
assessedWindows3D
assessedWindows3D =
assessedWindows3D(:,:,1) = 0.4593 0.8598 0.1560 0.5102 0.8747 0.8497 0.6789 0.1773 assessedWindows3D(:,:,2) = 0.8688 0.0926 0.0603 0.0062 0.2144 0.5245 0.8124 0.3713
assessedWindows3D(:,toRemove,:) = []; % note this is indexing toRemove in the second dimension
assessedWindows3D
assessedWindows3D =
assessedWindows3D(:,:,1) = 0.4593 0.1560 0.8747 0.6789 assessedWindows3D(:,:,2) = 0.8688 0.0603 0.2144 0.8124
  3 Comments
Christopher McCausland
Christopher McCausland on 11 Nov 2021
Hi Dave,
Thank you for taking the time to answer that! It makes so much more sense now! Really good examples too!
Irronically I have used linear indexing before, but hadn't considered thats what MATLAB was doing. With your explanation this makes sense though!
Thank you so much!
Christopher

Sign in to comment.

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!