is there an easy way to select let's say every 2 elements from an array taking the first nonzero element. My array is sparse double. For example:
val =
(1,1) 0.1667
(2,1) 0.1667
(3,1) 0.1667
(4,1) 0.1667
(5,1) 0.1667
(6,1) 0.1667
(7,1) 0.1667
(8,1) 0.1667
So I want to run my code taking 2 values in, next two values out and so on. Like this:
val =
(1,1) 0.1667
(2,1) 0.1667
(5,1) 0.1667
(6,1) 0.1667
Below is my code, let's say I want to select every 2 elements starting from the first nonzero element.
results=zeros(86400,1); % time of the day in seconds
for i=1:28,
currentFlowArray=allFileMin(i).demand_pattern1.Wm.total.flowArray;
for p=1:86400,
results(p)=results(p)+ currentFlowArray(p);
end
end
Hope I was clear! Thank you in advance!
To get the values out of val
:
valnz = nonzeros(val);
result = valnz(sort([1:4:nnz(val) 2:4:nnz(val)]));
To remove make the non-desired values within val
:
ind = find(val);
val(ind(sort([3:4:numel(ind) 4:4:numel(ind)]))) = 0;