How to replace the values of a vector from desired location to its end?

I have three vectors a, b and c given below. I want to replace all the values of 'a' from my desired location say for example from location 60 to its end such that those new values are asympotically decreasing from its previous values (i.e., location 1 to 59) and are also less in values from the values of vector b and c from the same location i.e., 60 to the last. One method is manual but its very time consuming. I want that MATLAB should do it for me.
a=[0.397514402866404 0.349231851271051 0.425836326161108 0.418823411789294 0.386696621928579 0.384575525674989 0.364845287492425 0.495928922548383 0.479694820530569 0.389582032512016 0.384505211980922 0.333724865928879 0.365919184423201 0.400772506172964 0.379242722219023 0.364459695600443 0.366128522849086 0.385174773969786 0.372867063392224 0.376548774132535 0.473071808102272 0.251627836852150 0.385042011392780 0.381148165802822 0.369065536223563 0.431444009941580 0.414879010564463 0.325227505916048 0.437887598814955 0.428837027000730 0.406640068369860 0.409409452088121 0.424434292672168 0.409932466026339 0.412835839983982 0.399245885507425 0.387575414369059 0.407050454183416 0.326211795299434 0.458434548036028 0.338455259025878 0.368562202826825 0.413767382594240 0.458452216705265 0.432455960242579 0.415225789491909 0.389509369547427 0.408404223427086 0.320887810417104 0.419718890158122 0.411956096059551 0.467265991314386 0.425563572738537 0.457286988716662 0.364096160999733 0.417395179173226 0.415151032279429 0.439401358954262 0.416025807219374 0.349557755864473 0.413126979577490 0.382242156796521 0.387869720649921 0.344424085502606 0.418357738224712 0.393375703492509 0.410207735948398 0.432538723781336 0.460651539221876 0.366947323455358 0.343158385463345 0.360779192277814 0.407775542134092 0.401720168768703 0.411858470659967 0.390296201351616 0.386700151754722 0.393559416101593 0.405571260599836 0.426038375438873 0.351052078565856 0.417357035534662 0.441552934076875 0.400293564991402 0.396957107193780 0.357192556998893 0.431047692995460 0.383175812178153 0.446884989308566 0.441112998092938 0.421424171377919 0.426723330306019 0.311409378918779 0.383676813236671 0.312569975506430 0.429482861741989 0.411346818807958 0.351598372195988 0.387493340782735 0.464061121524104];
b=[0.551682651670431 0.645096882777877 0.581684008069165 0.506704132814463 0.576791071314667 0.598095975405639 0.556468410315606 0.603215166364727 0.573881723497337 0.702829523545135 0.655401596692394 0.637834736633294 0.569663359602261 0.634042823056570 0.520415579093006 0.557141591176942 0.583010935305893 0.520542027714726 0.593618261801875 0.491359612337422 0.541672987084530 0.623432585679211 0.486386829534579 0.604580199387316 0.673723294633068 0.547784751842482 0.633068049834673 0.592470280261216 0.635036043068146 0.584179124192050 0.566186813602900 0.590306702135361 0.592142837346948 0.645760363411235 0.529148516876326 0.542359452487705 0.576111765619481 0.440572920274603 0.673125819553129 0.552989311869312 0.810202391087325 0.616215986476514 0.637502718705395 0.510482159785954 0.636855535171597 0.640515935733207 0.614862120663137 0.602807491865895 0.576035793127878 0.591165927487269 0.574446655730001 0.569647577308512 0.770477592901505 0.555931615848446 0.559585144340360 0.580838963787409 0.675838792335884 0.539290120744169 0.630342008576237 0.614578423728421 0.514912997133050 0.570801392261179 0.557904302582161 0.516333537322627 0.564906015777891 0.578745862292832 0.633282156008040 0.566117556674110 0.620214334996768 0.603670500271155 0.574218976104897 0.567667037444456 0.509795475612059 0.523914908058583 0.636086852547285 0.552641580772836 0.522137540787447 0.499410785060553 0.537599355871173 0.740725123231447 0.646255524703763 0.616982399812298 0.691986273761024 0.661806826749363 0.586985183103399 0.552227356972073 0.576677035029797 0.535296631878600 0.560018190112691 0.616588660122031 0.674001758525734 0.539967859639179 0.515501886822976 0.552872905239819 0.575552960829275 0.528735725333748 0.673536000041314 0.616306055966220 0.596732320262772 0.620430313847279];
c=[1.11459952996087 1.68670358870423 1.22532598107866 0.624815936271779 0.554255279167175 1.15749032120038 0.572704247243950 0.818472371403532 0.610108820878572 0.520393200082739 0.877109078448262 0.584376156817110 0.864813871234754 0.737829446874604 0.580997699895747 1.77068479618745 0.539182799387493 1.62437864878862 0.521003914004704 0.849895522917450 0.577263897904287 0.526984303503854 1.64990069405968 1.53080783202811 0.896341460688281 0.691820506571966 0.839658467823829 0.916296633939690 1.57481829415576 1.11756769475573 0.496358173351652 0.758366460593150 0.603591262006390 0.856744750071039 0.634025989346108 0.862693031299083 0.499801270339618 1.31880150505211 0.497983284511088 0.955339233571990 0.607304623078370 0.720711362625385 0.662375530565266 0.794992908912330 1.27508484531957 0.681838541650462 1.39432785905121 0.921767072964069 0.732218886357660 1.62092196497150 1.05716708049553 0.921938203224915 0.972931972055981 0.785631875908351 0.762332383313575 0.939098232445008 0.495829657412097 0.734698073565044 0.854786553397547 0.766021749291286 0.757263163382394 0.528035422450610 1.76412942257156 0.480878637061892 0.779087865119777 0.444875526901451 0.702140015667567 0.475393228042059 0.768312696667354 0.638589873668904 1.27460847277030 1.48937825107580 0.495231950051789 0.493444405935982 0.504732624749794 0.631618660777873 1.27292293987335 0.628781242055882 0.488060785261759 0.554757947749088 1.66634519190679 0.900376834938410 1.10921191147181 1.66756865514712 0.574270568683960 1.70204892135356 0.920784050984837 0.538263886964218 1.76503134659234 0.628734987014323 0.595634664706628 0.509007492420166 0.621106010756416 0.860695885662515 1.58470245060019 0.742921218056206 0.756422431551545 0.640398131277412 1.29779726790267 0.523270699017041];

7 Comments

I've plotted your values here. Replacing values from 60 to the end is easy, because you can just refer to
a(60:end)
But your description of how you want to replace them is unclear to me.
What do you mean by "asympotically decreasing"? Decreasing to what value? Zero? Do you have a formula for how to calculate the 60:end values from the 1:59 values?
(All the values of a are less than all the values of b and c, so that part seems to be an unnecessary consideration.)
a=[0.397514402866404 0.349231851271051 0.425836326161108 0.418823411789294 0.386696621928579 0.384575525674989 0.364845287492425 0.495928922548383 0.479694820530569 0.389582032512016 0.384505211980922 0.333724865928879 0.365919184423201 0.400772506172964 0.379242722219023 0.364459695600443 0.366128522849086 0.385174773969786 0.372867063392224 0.376548774132535 0.473071808102272 0.251627836852150 0.385042011392780 0.381148165802822 0.369065536223563 0.431444009941580 0.414879010564463 0.325227505916048 0.437887598814955 0.428837027000730 0.406640068369860 0.409409452088121 0.424434292672168 0.409932466026339 0.412835839983982 0.399245885507425 0.387575414369059 0.407050454183416 0.326211795299434 0.458434548036028 0.338455259025878 0.368562202826825 0.413767382594240 0.458452216705265 0.432455960242579 0.415225789491909 0.389509369547427 0.408404223427086 0.320887810417104 0.419718890158122 0.411956096059551 0.467265991314386 0.425563572738537 0.457286988716662 0.364096160999733 0.417395179173226 0.415151032279429 0.439401358954262 0.416025807219374 0.349557755864473 0.413126979577490 0.382242156796521 0.387869720649921 0.344424085502606 0.418357738224712 0.393375703492509 0.410207735948398 0.432538723781336 0.460651539221876 0.366947323455358 0.343158385463345 0.360779192277814 0.407775542134092 0.401720168768703 0.411858470659967 0.390296201351616 0.386700151754722 0.393559416101593 0.405571260599836 0.426038375438873 0.351052078565856 0.417357035534662 0.441552934076875 0.400293564991402 0.396957107193780 0.357192556998893 0.431047692995460 0.383175812178153 0.446884989308566 0.441112998092938 0.421424171377919 0.426723330306019 0.311409378918779 0.383676813236671 0.312569975506430 0.429482861741989 0.411346818807958 0.351598372195988 0.387493340782735 0.464061121524104];
b=[0.551682651670431 0.645096882777877 0.581684008069165 0.506704132814463 0.576791071314667 0.598095975405639 0.556468410315606 0.603215166364727 0.573881723497337 0.702829523545135 0.655401596692394 0.637834736633294 0.569663359602261 0.634042823056570 0.520415579093006 0.557141591176942 0.583010935305893 0.520542027714726 0.593618261801875 0.491359612337422 0.541672987084530 0.623432585679211 0.486386829534579 0.604580199387316 0.673723294633068 0.547784751842482 0.633068049834673 0.592470280261216 0.635036043068146 0.584179124192050 0.566186813602900 0.590306702135361 0.592142837346948 0.645760363411235 0.529148516876326 0.542359452487705 0.576111765619481 0.440572920274603 0.673125819553129 0.552989311869312 0.810202391087325 0.616215986476514 0.637502718705395 0.510482159785954 0.636855535171597 0.640515935733207 0.614862120663137 0.602807491865895 0.576035793127878 0.591165927487269 0.574446655730001 0.569647577308512 0.770477592901505 0.555931615848446 0.559585144340360 0.580838963787409 0.675838792335884 0.539290120744169 0.630342008576237 0.614578423728421 0.514912997133050 0.570801392261179 0.557904302582161 0.516333537322627 0.564906015777891 0.578745862292832 0.633282156008040 0.566117556674110 0.620214334996768 0.603670500271155 0.574218976104897 0.567667037444456 0.509795475612059 0.523914908058583 0.636086852547285 0.552641580772836 0.522137540787447 0.499410785060553 0.537599355871173 0.740725123231447 0.646255524703763 0.616982399812298 0.691986273761024 0.661806826749363 0.586985183103399 0.552227356972073 0.576677035029797 0.535296631878600 0.560018190112691 0.616588660122031 0.674001758525734 0.539967859639179 0.515501886822976 0.552872905239819 0.575552960829275 0.528735725333748 0.673536000041314 0.616306055966220 0.596732320262772 0.620430313847279];
c=[1.11459952996087 1.68670358870423 1.22532598107866 0.624815936271779 0.554255279167175 1.15749032120038 0.572704247243950 0.818472371403532 0.610108820878572 0.520393200082739 0.877109078448262 0.584376156817110 0.864813871234754 0.737829446874604 0.580997699895747 1.77068479618745 0.539182799387493 1.62437864878862 0.521003914004704 0.849895522917450 0.577263897904287 0.526984303503854 1.64990069405968 1.53080783202811 0.896341460688281 0.691820506571966 0.839658467823829 0.916296633939690 1.57481829415576 1.11756769475573 0.496358173351652 0.758366460593150 0.603591262006390 0.856744750071039 0.634025989346108 0.862693031299083 0.499801270339618 1.31880150505211 0.497983284511088 0.955339233571990 0.607304623078370 0.720711362625385 0.662375530565266 0.794992908912330 1.27508484531957 0.681838541650462 1.39432785905121 0.921767072964069 0.732218886357660 1.62092196497150 1.05716708049553 0.921938203224915 0.972931972055981 0.785631875908351 0.762332383313575 0.939098232445008 0.495829657412097 0.734698073565044 0.854786553397547 0.766021749291286 0.757263163382394 0.528035422450610 1.76412942257156 0.480878637061892 0.779087865119777 0.444875526901451 0.702140015667567 0.475393228042059 0.768312696667354 0.638589873668904 1.27460847277030 1.48937825107580 0.495231950051789 0.493444405935982 0.504732624749794 0.631618660777873 1.27292293987335 0.628781242055882 0.488060785261759 0.554757947749088 1.66634519190679 0.900376834938410 1.10921191147181 1.66756865514712 0.574270568683960 1.70204892135356 0.920784050984837 0.538263886964218 1.76503134659234 0.628734987014323 0.595634664706628 0.509007492420166 0.621106010756416 0.860695885662515 1.58470245060019 0.742921218056206 0.756422431551545 0.640398131277412 1.29779726790267 0.523270699017041];
figure
hold on
plot(a)
plot(b)
plot(c)
legend(["a","b","c"])
Thanks a lot dear the cyclist for your kind response. Yes all values of 'a' are less than 'b' and 'c' in this case but values of 'c' are less than 'b' which is wrong after about 80 on ward. Values of 'b' must be less than 'c'. The order of values of all vectors should be: a<b<c. There is no specific formula but as the starting value of 'a' is initial value and the final value should be the desired location value. So all the new values of 'a' from location 60 to end must be in between these two values but in decreasing order i.e., value of location 60 must be greater than location 61. Likewise value of location 61 must be greater than location 62 and so on till the end. The same fromla will be used for 'b' i.e., starting value of 'b' is initial value and location 60 value is the final value and all new values from location 60 to last should be in between those two values. The same formula is for 'c'. However if all values of vector 'a' are less than b and c, then there should be no replacing of old values in vector 'a'. Replacing of values by new values should be applied to the vector if it violtes the standard rule which is a<b<c.
The desired location should be the place where the value of 1st vecor i.e., 'a' becomes greater than the corresponding value in the same location in vector b and c for vecor a. Likewise the desired location for 'b' must be the location in b where its values becomes greater than the same location in vector 'c'.
Can you explain why it is a problem that b>c "after about 80", but it is not a problem that b(5) > c(5)?
How should the exact location to start fixing the problem be selected? How do we figure out "about 80"?
It might be helpful for you to explain the reasoning behind changing this vectors, because the rules are very strange.
Thanks a lot for your kind response dear the cyclist. Actually when I arrange all the three vectors in descending order and then plot them using the semilogy() command, then it is visible to me from the graph that where the values of a vector increases or decreases and at which location. See the attached graph. Its a stochaic process and cann't be predicted. However, I mean can we write some thing for-loop and if-elseif statements that can do the purpose i.e.,
for coun=1:100
if a(ii)>b(ii)||c(ii)
a(ii)=initial_value+rand(ii+1:end)*(final_value-initial_value);
end
end
I am not too much expert but can we do something like this for all the three vectors that can do my work? I cannot make the logic.
There are values of c that are less than b way back before index 20, not ony at the higher indices.
What's the justification for the uniformly decreasing sequence between two sets of indices; certainly the waveforms presented don't show any such patterns.
My recommendation would be to simply adjust each of b to be greater than a at each point by some fraction of the difference or randomized amount, then repeat that for the c trace relative the adjusted b (in the cases b needs adjusting, too).
Thanks a lot dear dpb for your kind response. I understand your logic but I cannot implement it in MATLAB as I am not too much expert.
Ok let me make it more simple. Can you write a code which 1st arrange all the vectors in descending order like below:
a=sort(a,'descend');
b=sort(b,'descend');
c=sort(c,'descend');
Then plot it like this:
Allvectors=[a b c];
semilogy(length(a),Allvectors);
Now if we see from the graph, the locations where vector 'a' becomes greater than 'b or c' and replace the values of a from that location onward by the above formula. Likewsie the location where b value becomes greater than c and note that location and replace values of b from that location onward? You can take any dummy location between 1 to 100. Say for example take location 20 for vector a and location 35 for b. Then replace the values of a and b according to the following formula:
a(20:end)=a(1)+(a(10)-a(1))*rand((100-20));
b(35:end)=b(1)+(b(10)-b(1))*rand((100-35));
Is this correct? will it do my work?
I did like this:
clear all; clc
%%%%%%%%%%%%%%%%%%%%%%%%
% Descending order
%%%%%%%%%%%%%%%%%%%%%%%%
a=sort(a,'descend');
b=sort(b,'descend');
c=sort(c,'descend');
Allvectors=[a b c]
b(80:end)=b(0)+(b(80)-b(0))*rand(100-80);
Allvectors=[a b c]
semilogy(length(a),Allvectors,'linewidth',2)
But it gives me the following error:
Array indices must be positive integers or logical values.
Error in ReplacingValues (line 13)
b(80:end)=b(0)+(b(80)-b(0))*rand(100-80);

Sign in to comment.

 Accepted Answer

The log scale plot was helpful to visualize the situation...good idea, but then you're far more aware of what the data are than we can possibly be so not surprising nobody thought to do so...of course, that you didn't attach a data file originally didn't help... :)
Anyways, if the point is to simply adjust the traces so they're all always ordered relative to each other, then how about simply coding in sequence to do so by something like
AdjFactor=100;
cprime=max(c,b.*(1+rand(size(c))/AdjFactor.*b));
The above will pick c whenever it is the larger but then replace c by some pickable scaling factor over the lower for the points that are lower than c. It won't change anything in the original lower (b) nor anything in the upper (c) that is already larger; the failing points will be some randomly perturbed copy of the original b in those locations but always at least some greater than the original b. It does not, however, have any idea that the values between successive points that are replaced are uniformly decreasing -- but, I don't think that is really what you meant to be -- decreasing within the trace; but only that the traces should be ordered at each location amongst the three.
Apply the above sequentially for a and b first, then use the revised b in the second comparison for bprime,c.
If you don't need the original vectors any longer after adjustment, then you can just write c on the LHS above, of course (and similarly for b in the prior comparison).

5 Comments

Thanks a lot dear dpb for your kind response. Amazing. Its very interesting. But can you explain how you choose the " AdjFactor=100; "?
Further, how is the following line works, I don't understand its logic:
cprime=max(c,b.*(1+rand(size(c))/AdjFactor.*b));
Besides this I think its automatically done. I mean it sees where the value of 'c' becomes less than b, it automatically increases it irrespective of the location dictation. Isn't that?
The adjustment scaling factor is totaly aribtrary; I just stuck it in there for you to play with and decide how large of a perturbation you wanted to apply. As written it's a divisor on the magnitude returned from rand() which is [0-1] so it's applying 1% of the magnitude of b at the location as the adjustment.
The logic is as explained in the comments in the answer; max(x,y) for vectors x and y selects the maximum of the two on a point-by-point comparison basis.
Actually, on further reflection, to meet the problem specification precisely the solution should be modified slightly to first compute the logical vector of (c<=b) and then make the substitution only on those points; as written, it's possible that for a given point (c>b) is true but the adjusted b is >c so it would make the substitution there as well.
To do it that way is also pretty straightforward; you just compute the indexing vector first...
isLess=(c<=b); % logical array of locations second <= first
c(isLess)=b(isLess).*(1+rand(sum(isLess),1)/AdjFactor.*b(isLess)); % replace those locations only
How often the above will be different than the max solution will vary with the size of the adjustment factor, the smaller it is, the more often the two will differ. I think on retrospect I'd recommend the latter.
Nota Bene: the above is replacing c in place; if you need to also keep the original c then make a copy first --
cprime=c;
then substitue in cprime for c on LHS of the above.
Thanks a lot dear dpb for your ind explanation. Yes, I got it now. But is it possible in MATLAB that we write a code and we just give it three vectors like above and it itself calculates the adjustment factor and do the changes where required i.e., where the 2nd vector becomes less than the 1st and it increases its value automatically i.e., where b becomes less than a, it automatically increases b values at that location and then proceeds and find any other location and adjusts its value where the same happens. Likewise where c becomes less than b and it increases value of c at that location automatically and at the last we get the modefied vectors which are according to the rule a<b<c ?
i.e., [a' b' c']=adjustlikefunction(a,b,c)
where a', b' and c' are modified vectors. Is it possible?
That's what we just did....your mission, should you choose to accept it, is to take the above logic and wrap a function around it if you so choose to use it in that manner.
Thank you very much dear dpb for your kind response. Ok, that's great.

Sign in to comment.

More Answers (1)

The err in your code is with 0 in b's index that is not acceptable. Because b(0) does not exist. Which element of b vector are you try to call there? E.g 1st element, then b(1)

1 Comment

Thanks a lot dear Sulaymon Eshkabilov for your kind response. Yes you are right. It worked but the actual problem is still there i.e., the one that has been posted.

Sign in to comment.

Asked:

on 15 Jan 2023

Commented:

on 18 Jan 2023

Community Treasure Hunt

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

Start Hunting!