how to read\scan all data of a row present in an array and pick data according to their priority number which is placed in next column of that particular row?

1 Ansicht (letzte 30 Tage)
hi... i have an array which has 5 rows and in second coulmn I placed their priority (1 to 10) higher number means higher priority now i have an another array in which I want to place these arrays according to their priority.
for example: A[ 1.2160 7
0.8931 10
-0.8498 2
1.6598 6
0.3412 9]
B [ 0.310 7
0.2080 2
0.0466 1
0.7997 4
0.8229 5]
A is an array in which each data has their own priority between 1 to 10. Now how I find out the data with higher priority and put that specific value in array B by replacing the value present in that array that has LRU value less than 7 in sequence ( for example from array B first data with lru number 1 is evicted first and from array [A] data with priority 10 replaced it , then data with lru 2 is evicited and from array A anotther data replace it with higher priority , then data with lru 3 if any data has till 7. data with lru 7 is not replaceable ). I only want to place that specifice data in another array not their priority number.
result array should be Resulting array [ 0.310
0.3412
0.8931
1.2160
1.6598 ]
basically data with priority 10 replace data of array B with lru 1, data with priority 9 replace data of array B with lru 2, data with priority 7 replace data of array B with lru 4 data with priority 6 replace data of array B with lru 5... data of array with lru 7 remains on its position becuase it has lru 7
how can I do this in a sequence with loop when i have a large amount of data then how to do it ? please any one help me out .
how to do the same process with arrays with different dimesions? fro example if A has 20 rows of data with their priority and array B has 5rowsof data with their lru. then how to replace data from array A according to their priority with data in array B with less lru?

Akzeptierte Antwort

Askic V
Askic V am 20 Feb. 2023
Bearbeitet: Askic V am 20 Feb. 2023
Hello Saira,
for this particular case, asumming that priorities go between 1 and 10, this code can work:
A = [1.2160, 7;
0.8931, 10
-0.8498, 2;
1.6598, 6;
0.3412, 9];
B = [0.310, 7;
0.2080, 2;
0.0466, 1;
0.7997, 4;
0.8229, 5];
for i = 1: size(B,1)
if B(i,2) < 6
ind = 11 - B(i,2);
B(i,1) = max(A(A(:,2) == ind));
end
end
result_arr = B(:,1)
result_arr = 5×1
0.3100 0.3412 0.8931 1.2160 1.6598
This code will modify B as you explained in the description, but since only an array is expected, then the first column of modified B is extracted. If you want to know why 11 and 6, think about max priority and 1/2 of the range.
Anyway, I think you'll have little trouble or not trouble at all to modify this code to suit your actual needs.
  3 Kommentare
Askic V
Askic V am 28 Feb. 2023
Please post more realistic matrices A and B and the expected outcome. Then it will be easier to see what needs to be modified in the existing code.
Saira
Saira am 1 Mär. 2023
Bearbeitet: Saira am 1 Mär. 2023
sortedprioritydata [1.21602018873774 10
-2.22909989155828 10
0.893185966969769 9
1.02908840545717 9
-0.849841587777029 8
2.72704966338821 8
1.65989076612967 7
1.03549829028579 7
0.341240128622871 6
-0.0135126518557689 6
-1.33608880779521 5
-1.80890646939778 5
-1.82840584716151 4
-0.0692502489286168 4
0.810804787327033 3
2.08083166079756 3
3.19758575067649 2
-2.55150643398200 2
2.52199368943212 1
1.94779923722415 1]
this is the main matrix from which we have to place these data into 3 caches named as cache1 cache2 and cache3 in sequence. caches holds data with lru 0 to 7 . size of cache1 is 5,, cache2 size is 7 and cache3 size is 8. now first we place data from main array to cache1 by evicting data whoose lru is <7. if any data in cache1 don't have lru 7 then all will be removed and higher data priority value will place in sequence as you did in above example. similarly when cache1 is full we will go to next cache which is cache2. if first 5 rows of main array replaced the cache1 data then that data will never entered again in cache2 . for example if first 5 data of main array (1.21602018873774, -2.22909989155828, 0.893185966969769,1.0290884054571,-0.849841587777029) replace the data in cache 1 then these values will never enetr again in cache2 similarlry data that replace cache2 data will never enter in cache3.
cache1 [0.814723686393179 0
0.905791937075619 1
0.126986816293506 2
0.913375856139019 3
0.632359246225410 4]
cache2 [0.0975404049994095 0
0.278498218867048 1
0.546881519204984 2
0.957506835434298 3
0.964888535199277 4
0.157613081677548 5
0.970592781760616 6]
cache3 [0.957166948242946 0
0.485375648722841 1
0.800280468888800 2
0.141886338627215 3
0.421761282626275 4
0.915735525189067 5
0.792207329559554 6
0.959492426392903 7
0.655740699156587 1]
for this purpose 1 divided the main array into three chunks chunk1 chunk2 and chunk3. cache1 and chunk 1 has same dimesion 5×2 , cache2 and chunk2 has same dimension 7×2 and cache3 and chunk3 has same dimension 8×2. when I performed the above code to compare chunk1 with cache1 , chunk2 with cache 2 and chunk3 with cache3 it gives error. chunk1 holds first 5 data from main array from inedx 1 to 5. chunk2 holds data from index 6 to 12 and chunk3 holds data from index13 to 20 from main array.
chunk1 [1.21602018873774 10
-2.22909989155828 10
0.893185966969769 9
1.02908840545717 9
-0.849841587777029 8]
chunk2 [2.72704966338821 8
1.65989076612967 7
1.03549829028579 7
0.341240128622871 6
-0.0135126518557689 6
-1.33608880779521 5
-1.80890646939778 5]
chunk3 [-1.82840584716151 4
-0.0692502489286168 4
0.810804787327033 3
2.08083166079756 3
3.19758575067649 2
-2.55150643398200 2
2.52199368943212 1
1.94779923722415 1]
it gives the error I mentioned above and also gives different sizes but all these have same dimensions. kindly help me out to perform such operation thanking in advance.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by