Borrar filtros
Borrar filtros

Modifying the array with numerical values in the vicinity of the elements in earlier array

2 visualizaciones (últimos 30 días)
Hello,
I don't know if it is clear from title what I actually want to ask.
I am trying to plot rectangular pulses. I simple dont want any of the plotted pulses to overlap.
The thing is, the start times of the pulses are calculated from some program. When I find the pulses for next user then these instances are overlapping (in multiple ways) with the earlier instances. Following is the code.
GlobIWC=zeros(1,86400); % First I am defining global instance vector
NZGlobIWC=find(GlobIWC) % Global Instance Start time vector-Only non zero elements of Global vector are extracted.
NZGlobFWC=find(GlobIWC) % Global Instance End time vector
% These vectors are just to initialize the loop in the program
users=[1 2 3 4 5 6]
timestep=0:0.00027778:24;
for user=1:length(users)
% some code
WCId=[3437 68688 77158 79405] % I get these location of instances on time scale(0-24 hrs) from 1 to 86400 for this current user
Dur=240; % this will be the duration of Pulse in seconds
FWCId=WCId+Dur; % this will be the end time location of the pulse
FWCId=[34619 68928 77398 79645] % this is End instance vector
% Overlap removal loop
for jj=1:length(WCId)
for ii=1:length(NZGlobIWC)
if ((NZGlobIWC(ii)-Dur) < WCId(jj)) && (WCId(jj)< (NZGlobFWC(ii)+Dur)) || ((NZGlobIWC(ii)-Dur) < FWCId(jj)) && (FWCId(jj)< (NZGlobFWC(ii)+Dur))
WCId(jj) = NZGlobFWC(ii) + Dur +randi ([240 600]); %In this middlmost loop I tried to use "for"instead of "if"
end
end
end
% In above loop, I try to block the Time location from Global Non-zero vector in a passage (NZGlobIWC-dur) to (NZGlobIWC-dur) for
% both start time (WCId) and end time (FWCId) of the New pulse and assign WCId a new value which is some random time after the end of
% earlier pulse
for ii=1:length(WCId)
GlobIWC(WCId(ii))=WCId(ii); % Here I add the new elements to the global instance of use vector,
end
% Some code
Dur= 120; % Now the new value of Dur is calculated here.
NZGlobIWC=find(GlobIWC); % The new Non zero instance vectors for both start and end time are created here
NZGlobFWC=NZGlobIWC+Dur;
for kk=1 :length(WCId)
FlowWC(WCId(kk):(FWCId(kk)))=6;
end
plot(timestep,FlowWC);
hold on
end
One thing I found is, in overlap removal loop, I check the current instance(WCId) with Global instances(NZGlobIWC), but I dont check this newly formed instanes (modified WCId) again with the NZGlobIWC. This is what might be causing problem. I tried to use "while" loop in the Overlap removal. But it takes like forever to end the loop . As this is just a very small part of my program I need computationally efficient way to resolve this seemingly complex issue.
How can I stop overlapping of these pulses?
Kindly help.
P.S. Attached in the expected figure .

Respuestas (1)

Nipun
Nipun el 30 de Mayo de 2024
Hi Rohit,
I understand that you want to modify an array with numerical values in the vicinity of elements in an earlier array. Here’s how you can do it:
% Given array
originalArray = [1, 5, 10, 15];
% Range for modification
range = 2; % Modify within +/-2 of each element
% Modify array
modifiedArray = arrayfun(@(x) x + randi([-range, range]), originalArray);
disp(modifiedArray);
This code modifies each element of originalArray by adding a random integer within the specified range.
For more details, refer to the MathWorks documentation on arrayfun: https://www.mathworks.com/help/matlab/ref/arrayfun.html
Hope this helps.
Regards,
Nipun

Categorías

Más información sobre Logical en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by