Let's say I have a cell array raweeg each cell of which is a matrix with time points in the first column and some markers in the second. I want to create a vector to store the time points, when the marker is not equal to -1. I found a non-elegant (and not working) way to create a vector of zeros of size 1x1 and then to append the following values in a loop.

P.S.: There are exactly 96 non-"-1" values and corresponding time points.

startpoints = zeros(1,1);

for i = length(raweeg{1,1}(:,1))
   if raweeg{1,1}(i,2) ~= -1
   startpoints(end+1,1) = raweeg{1,1}(i,1); 
   end
end

Thank you

有帮助吗?

解决方案

Vectorize it like this, for a given cell of raweeg:

startpoints = raweeg{1,1}(raweeg{1,1}(:,2) ~= -1, 1);

This is called logical indexing.

Just be sure your markers are not generated with floating point computations, or the comparisons will likely fail often.


P.S. The problem in your code is the for loop statement, which should be:

for i = 1:length(raweeg{1,1}(:,1))

Or better, for i = 1:size(raweeg{1,1},1).

With out the "1:" part, it just has a single iteration, the last row.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top