Filter löschen
Filter löschen

Why does this technique not work?

137 Ansichten (letzte 30 Tage)
Sadiq Akbar
Sadiq Akbar am 1 Aug. 2024 um 7:16
Kommentiert: Torsten am 2 Aug. 2024 um 12:57
I have a mat file which has a row vector u and a matrix temp_gbo. The size of this matrix is 100 x 4. I want the following:
Each row of the matrix temp_gbo must have the same arrangment of elements as is in u.The mat file and the script "swapping" are attached but the script doesn't fulfill my requirement. How should I modify this code?
clear;clc
load 2F_and_1J_Noise0
temp_gbo
temp_gbo = 100x4
88.4028 38.5065 46.0277 4.7098 38.5561 64.7773 75.2408 3.0414 60.7072 31.7335 84.1475 0.4325 51.2496 179.9745 75.4241 1.9322 36.0663 91.9651 5.9596 2.4260 27.8965 61.4317 81.2432 4.9581 54.8198 33.0297 38.9350 0.4600 51.3407 38.9219 74.6855 0.4327 155.7973 48.8800 66.5703 3.9794 37.2537 86.8754 9.0043 4.3471
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
u
u = 1x4
33.0000 60.0000 80.0000 1.2000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% Create an index mapping based on the ordering in u
[~, idx] = sort(u)
idx = 1x4
4 1 2 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% Rearrange each row of temp_gbo to match the order in u
sorted_temp_gbo = arrayfun(@(i) temp_gbo(i, idx), 1:size(temp_gbo, 1), 'UniformOutput', false);
% Convert cell array back to matrix
sorted_temp_gbo = cell2mat(sorted_temp_gbo');
% Display results
disp('Rearranged temp_gbo:');
Rearranged temp_gbo:
disp(sorted_temp_gbo);
4.7098 88.4028 38.5065 46.0277 3.0414 38.5561 64.7773 75.2408 0.4325 60.7072 31.7335 84.1475 1.9322 51.2496 179.9745 75.4241 2.4260 36.0663 91.9651 5.9596 4.9581 27.8965 61.4317 81.2432 0.4600 54.8198 33.0297 38.9350 0.4327 51.3407 38.9219 74.6855 3.9794 155.7973 48.8800 66.5703 4.3471 37.2537 86.8754 9.0043 4.3438 52.9683 20.9183 82.5033 4.7273 47.6448 15.3711 82.2237 0.9042 65.0012 27.4686 91.4664 4.6909 88.4891 31.2764 30.2764 1.0624 27.0809 58.7502 75.6295 4.2156 37.1013 89.1705 3.1686 4.0597 40.8265 85.0226 22.6161 2.6292 37.4192 64.3769 77.7092 2.4569 35.6162 68.1268 91.9500 3.3988 52.6940 0 79.5701 0.5169 24.7223 55.8234 72.4056 3.4761 85.5143 34.7750 0 1.9327 86.0476 39.6672 0.4948 4.7357 88.4182 34.5185 0.6006 2.0391 180.0000 50.1649 71.7617 4.5950 47.1916 91.6507 52.6494 4.0385 37.5877 85.3056 0 1.0422 57.8052 14.3757 73.2256 0.6285 31.9638 57.4676 47.1311 3.9067 31.8537 54.3263 54.9414 4.5741 41.7440 82.6068 0 5.0000 47.3462 180.0000 70.1522 4.2515 43.4673 84.9375 37.3158 0.7602 51.9906 28.7342 68.7879 2.2839 52.4258 29.8845 69.4983 4.0376 29.2728 61.5666 88.4511 2.8283 20.2754 56.0599 76.2128 3.9744 39.8841 86.5460 26.7284 2.3869 62.1685 36.2030 81.6113 1.8585 33.5541 59.2858 63.9540 4.2237 87.3762 37.1852 3.3405 4.9551 41.4211 49.4507 57.2578 2.7559 25.6862 48.5205 74.2897 3.7116 48.8804 33.2311 62.7674 0.9317 62.7828 36.0722 81.3585 1.2161 49.9005 65.6788 78.4954 1.4451 31.7006 55.3651 68.5411 3.6662 87.4762 36.8995 0 1.0496 93.8907 43.6019 13.1058 0.3931 53.0157 180.0000 61.1421 1.5544 34.4059 82.6804 0 5.0000 52.3391 16.2593 77.4597 0.9870 54.5280 33.4112 74.8179 3.2812 42.9704 86.6939 34.2715 1.3005 34.0263 62.9864 87.8448 3.9304 172.4801 54.4332 73.9627 0.3111 51.1589 8.6070 64.4341 2.4900 33.2038 90.9118 0 2.1520 37.0995 63.6866 84.1506 3.5267 87.2596 35.8582 4.7209 1.1037 85.0783 43.5476 8.4147 0.4104 46.3361 173.1743 0.7823 0.1996 71.1619 47.0687 95.7982 1.4369 92.2919 44.2698 17.8745 0.8587 180.0000 52.4932 70.1372 0.4115 57.2047 32.8462 64.8573 1.6112 33.0698 55.7516 71.6088 3.5221 51.8262 1.1651 70.1198 1.1918 47.5856 70.4000 86.2674 0.8800 70.2843 44.9222 94.0877 1.4681 93.8373 37.9288 39.5883 3.0089 85.4664 36.5468 0.2763 3.3264 89.7302 41.4511 37.6632 0.6193 50.7589 178.4640 42.7516 0.9991 56.8364 23.8414 65.1099 1.7919 53.3839 30.5285 81.9275 1.4039 60.3241 33.6689 73.5722 1.6628 56.9396 33.5052 74.8300 4.5622 35.9324 89.3568 51.3955 5.0000 49.7089 173.6502 82.2179 2.1364 68.1138 45.6288 80.4482 2.5751 86.8272 42.0998 23.0540 1.8130 40.4551 56.2903 66.2560 2.2854 50.5136 17.1055 71.8211 1.9451 40.4900 89.2407 3.8861 2.0499 19.2196 52.4555 72.9366 0.9984 25.8133 55.8932 72.7862 0.3682 0 54.5734 31.5113 3.2901 58.5582 33.0217 83.1362 0.6797 44.7811 0 39.2843 3.5797 54.9605 12.3977 81.3156 4.1099 40.6917 88.0154 25.5098 1.6345 87.2932 38.6123 14.3557 1.7437 53.3069 35.8710 71.3681 0.6821 51.2858 169.4714 69.2238 3.1260 37.3071 90.4495 2.1839 0.6866 56.7592 19.5314 83.4022 0.7968 97.2518 39.5505 38.2560 3.7702 5.6820 50.1955 72.5416 1.5873 48.2611 34.8206 48.1632

Akzeptierte Antwort

Torsten
Torsten am 1 Aug. 2024 um 10:30
Bearbeitet: Torsten am 1 Aug. 2024 um 11:01
I already gave you the code how to order a vector according to the ordering of "u". Why don't you use it ?
Your method is wrong. You get the order of the elements of u from the command
[~, idx] = sort(u)
as
[4 1 2 3]
Now if you order the elements of temp_gbo according to temp_gbo(i,idx), you put the element in column 4 in column 1, the element of column 1 in column 2, the element of column 2 in column 3 and the element of column 3 in column 4. But why should this reflect the ordering of u ? You don't use the ordering of the ith row of temp_gbo in any way.
  6 Kommentare
Sadiq Akbar
Sadiq Akbar am 2 Aug. 2024 um 10:50
Thanks a lot for your kind response. Yes, you are saying correctly.
I doubt you can use "arrayfun" in the correct version because you have to execute two commands in the loop: So can you modify my "arrafun" so that it does the same as does yours.
Torsten
Torsten am 2 Aug. 2024 um 12:37
Bearbeitet: Torsten am 2 Aug. 2024 um 12:39
So can you modify my "arrafun" so that it does the same as does yours.
I don't use "arrayfun", I use a loop. And I don't think that "arrayfun" is applicable in this corrected version of your code for the reason already mentionned.
load 2F_and_1J_Noise0;
u
u = 1x4
33.0000 60.0000 80.0000 1.2000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
temp_gbo
temp_gbo = 100x4
88.4028 38.5065 46.0277 4.7098 38.5561 64.7773 75.2408 3.0414 60.7072 31.7335 84.1475 0.4325 51.2496 179.9745 75.4241 1.9322 36.0663 91.9651 5.9596 2.4260 27.8965 61.4317 81.2432 4.9581 54.8198 33.0297 38.9350 0.4600 51.3407 38.9219 74.6855 0.4327 155.7973 48.8800 66.5703 3.9794 37.2537 86.8754 9.0043 4.3471
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
[~, iu] = sort(u);
sorted_temp_gbo = zeros(size(temp_gbo));
for i = 1:size(temp_gbo, 1)
[~,itemp(iu)] = sort(temp_gbo(i,:));
sorted_temp_gbo(i,:) = temp_gbo(i,itemp);
end
sorted_temp_gbo
sorted_temp_gbo = 100x4
38.5065 46.0277 88.4028 4.7098 38.5561 64.7773 75.2408 3.0414 31.7335 60.7072 84.1475 0.4325 51.2496 75.4241 179.9745 1.9322 5.9596 36.0663 91.9651 2.4260 27.8965 61.4317 81.2432 4.9581 33.0297 38.9350 54.8198 0.4600 38.9219 51.3407 74.6855 0.4327 48.8800 66.5703 155.7973 3.9794 9.0043 37.2537 86.8754 4.3471
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Abhas
Abhas am 1 Aug. 2024 um 7:52
Hi Sadiq,
To achieve the specified order based on the relative magnitudes within each row of "temp_gbo", you need to sort each row and then rearrange the elements according to the specified positions.
Here's the MATLAB code to perform the required steps:
clear; clc;
load 2F_and_1J_Noise0;
% Define the new order based on the relative magnitudes
new_order = [2, 3, 4, 1]; % 2nd lowest, 3rd lowest, 4th lowest, lowest
% Preallocate the sorted matrix
sorted_temp_gbo = zeros(size(temp_gbo));
% Rearrange each row of temp_gbo to match the specified order
for i = 1:size(temp_gbo, 1)
[~, sort_idx] = sort(temp_gbo(i, :)); % Get the indices of the sorted elements
sorted_temp_gbo(i, :) = temp_gbo(i, sort_idx(new_order)); % Rearrange according to new_order
end
% Display results
disp('Rearranged temp_gbo:');
disp(sorted_temp_gbo);
Output:
You may refer to the following MathWorks documentation link to have a better understanding on array indexing: https://www.mathworks.com/help/matlab/math/matrix-indexing.html
  4 Kommentare
Sadiq Akbar
Sadiq Akbar am 1 Aug. 2024 um 10:49
Thanks a lot for your kind response.
1-How did you decide the ordering of elements of u? I am not getting you.
2-What is the problem with my code. I want to use my code but how should I modify it to meet my requirement?
Abhas
Abhas am 1 Aug. 2024 um 11:12
  1. Determine the new positions of each element in u after sorting, and use this order to rearrange the elements in each row of temp_gbo.
  2. The command you used only provides the order of the elements in u, but it does not specify how to rearrange the elements in temp_gbo accordingly. In my code it specifies the order in which the smallest, second smallest, third smallest and largest element should be placed at.

Melden Sie sich an, um zu kommentieren.


Steven Lord
Steven Lord am 1 Aug. 2024 um 15:11
Setup
Let's generate a collection of possible rows.
u = randi(10, 6, 3);
Let's ensure that no row in u has the exact same values as another row, though perhaps in a different order.
s = sort(u, 2);
while height(unique(s, 'rows')) < height(u)
u = randi(10, 6, 3);
s = sort(u, 2);
end
u
u = 6x3
7 10 6 10 3 7 4 10 10 8 1 9 1 2 3 10 7 8
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Now create the matrix and reorder each row in A arbitrarily.
whichrow = randi(height(u), 8, 1);
A = u(whichrow, :);
for therow = 1:height(A)
A(therow, :) = A(therow, randperm(width(A)));
end
A
A = 8x3
3 7 10 7 10 6 10 3 7 10 4 10 8 10 7 10 7 6 10 7 6 4 10 10
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
The algorithm
We can use ismember (or you may want to use ismembertol if your data is not integer valued) to determine which row in u was used to generate each shuffled row in A. First we sort the rows in u and A then we ask whether each row in Asorted is present in s (we know that it is, so I can ignore that output) and which row of s the corresponding row of Asorted is.
s = sort(u, 2);
Asorted = sort(A, 2);
[~, ind] = ismember(Asorted, s, 'rows');
Trust but verify
To check, does this match the contents of whichrow, which is the variable we used to construct A in the first place?
[ind, whichrow]
ans = 8x2
2 2 1 1 2 2 3 3 6 6 1 1 1 1 3 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Looks good to me.
  2 Kommentare
Sadiq Akbar
Sadiq Akbar am 2 Aug. 2024 um 10:52
Thanks a lot for your kind response. I thought too much about your code but I am not getting you what have you tried to make me understand. I am sorry for that.
Torsten
Torsten am 2 Aug. 2024 um 12:57
Since i remember the previous question @Sadiq Akbar asked, his aim is to find code for the following problem:
Given two vectors u and v of length n, find the ordered vector v_ordered of length n such that if u(i) is the j-th biggest element of u, then v_ordered(i) is the j-th biggest element of v for 1 <= i <= n.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by