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

2 ビュー (過去 30 日間)
Sadiq Akbar 2023 年 1 月 15 日
コメント済み: Sadiq Akbar 2023 年 1 月 18 日
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 件のコメント5 件の古いコメントを表示5 件の古いコメントを非表示
Sadiq Akbar 2023 年 1 月 16 日

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?
Sadiq Akbar 2023 年 1 月 16 日
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);

サインインしてコメントする。

### 採用された回答

dpb 2023 年 1 月 16 日

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
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 件のコメント3 件の古いコメントを表示3 件の古いコメントを非表示
dpb 2023 年 1 月 18 日
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.
Sadiq Akbar 2023 年 1 月 18 日
Thank you very much dear dpb for your kind response. Ok, that's great.

サインインしてコメントする。

### その他の回答 (1 件)

Sulaymon Eshkabilov 2023 年 1 月 16 日

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 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
Sadiq Akbar 2023 年 1 月 16 日
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.

サインインしてコメントする。

### カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

### Community Treasure Hunt

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

Start Hunting!

Translated by