How to add values in coloumn with corresponding of logical array?
Mostrar comentarios más antiguos
I have a matrix which is shown in following snipped table.
I have to add the value in colomn no 7 when the logical value as per in last coloumn is 1 with the previous value of coloumn 7.
For example: In 3rd row of last coloumn I have logical value 1 so I can add corresponding value of coloumn 7 i.e, 7.6079 with previous value 4.1703 and replace the 4.1703 with output 11.7782.
For that wrote following code:
% Event is the Matrix as shown in the image.
St = find(Event(:,10)); % Index of ones in last coloumn
m = length(St); % No of instance
n = length(Event);
for i = 1:n-m
if Event(i,10) == 1
s = Event(i,7) + Event(i-1,7);
Event(i-1,7)=s;
Event(i,:)=[];
end
end
This worked for all the cases where logical 1 in last coloumn is followed by 0. But, problem encountered when there are consecutive 1 in last coloumn. The above code is adding the corresponding value of 7th coloumn with respect to last coloumn.
The output should be 77.8971(5.0724+12.730+10.910+19.562+24.285+5.3377). i.e, all the corresponding values of all consecutive 1 should be added with previous 0.
But I'm geting 17.8024 (5.0724+12.730).

Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!