Use sorted variable to reorder rows of a matrix

4 vues (au cours des 30 derniers jours)
James Peach
James Peach le 5 Jan 2021
Commenté : James Peach le 5 Jan 2021
I am attempting to reorder the rows of a matrix from greatest number of nonzero elements to least. I calculate the number of zeros per row and then sort that variable to get the correct order, but I am having tourble using my sorted variable to reorder the original matrix. I have posted what I have so far below
Index_matrix = [42 33 27 22 17 12 7 4 2 1 0
43 34 26 21 16 11 6 3 1 0 0
44 35 28 22 17 12 7 4 2 1 0
45 36 29 23 18 13 8 2 1 0 0
46 37 30 24 19 14 9 3 1 0 0
47 38 31 25 20 15 10 5 2 1 0
48 39 32 26 21 16 11 6 3 1 0
49 40 33 27 22 17 12 7 4 2 1
50 41 31 25 20 15 10 5 2 1 0];
test = sum(Index_matrix==0, 2);
test2 = sort(test,1);
index_matrix = sort(Index_matrix, test2);
  3 commentaires
James Peach
James Peach le 5 Jan 2021
1) I have two accounts. I created the second one (this one) because I perceived that people had stopped helping the other one for some reason. I guess I know why now lol.
2) I know some of my questions seem basic because some are, which is always embarrassing to me cause it just shows how much I've forgotten from back when I was taking matlab courses for homework in 2012 during my undergraduate degree. Most of what I'm doing now in the various questions I have asked either wasn't avaiable back then or had to be done in a more convoluted way.
3) I used to post my entire code, but having been told I code in a confusing way multiple times (due to my inexperience/ unfamiliarity with all matlab's capabilities). I decided to only post the code or piece specific to what I am struggling with if the problem didn't need all the other parts before it to be solved. When I first started posting I used to put the whole code cause that's what the forum FAQ said to do, but I've learned it's not always necessary and can make people less likely to help. Since, you can see all my questions asked you see how my asking format has changed.
4) I have posted solutions to some of my unanswered problems because I felt that someone might have my problem in the future and it would be helpful to them. Do the people that ask homework questions typically follow up weeks/months later to post the solutions to those questions back on here or are they usually never heard from again? Also, I know it's taboo to post solutions to your own questions because Ive read the FAQ/had it posted on my questions multiple times, but some of the problems I have had are very unique and required unqiue solutions that took time to figure out. I want to save future question askers that time.
5) I actually took a numerical methods course last semester for my master's degree, and got an A- instead of an A because I refused to ask for help on here for my hw and projects. I don't post homework questions on here cause I respect the community too much for all the help they gave me back in 2019 when I was just getting my sea legs back in regards to coding in Matlab. I apologize if any of my behavior has disrepected this wonderful platform that this community has created and maintained.
James Peach
James Peach le 5 Jan 2021
Here is the full code that generated the matrix above. The matrix is stored under the handle master_index. All of this code builds on all the previous code I have posted on here for my research.
% 3D to 2D Mapping, Labeling, Rescaling
clear
close all
clc
%A = 50 pts
A = [0,0,0;0.00977495601952172,0.0129188554738323,0.999868768093125;-0.566794094824837,-0.823750570492204,0.0133959578031223;0.0279587435128966,0.0380588867245362,1.99938731654588;0.830388266617646,0.583999369869120,0.978401571089338;-1.07826433834531,-1.64452537544960,0.267810795303313;0.168715496407312,0.263085998125572,2.96351922435162;-0.791458202545459,0.611268411797120,0.998070417818667;-1.41124221175034,-0.289407593850698,0.0506110237693017;1.69942938355116,1.04462646221878,1.15892918358242;-1.55216375501531,-2.44987966243271,0.623934110066297;0.188310075544404,0.501770043093754,3.93441879647330;-1.44603145623221,1.35107113546187,1.15371676572365;-2.35391403973316,0.0183196401577155,0.179737992978120;2.59879677816763,1.38804628951095,1.42948622326796;-1.92629974765512,-3.28302931738291,1.03122259685150;0.190461468181228,0.731318108759712,4.90771374511875;-2.01837467713375,2.14014570650778,1.37684130228734;-3.30531517848174,0.217844651708771,0.414313445558867;3.50709118523837,1.65551615551852,1.75113998255253;-2.23204460424917,-4.13488361866807,1.45650407075820;0.193343935566412,0.934427321909631,5.88686559182864;-2.51244231909718,2.97180232130260,1.63030617210704;-4.25650570961388,0.357419056329789,0.689550723301627;4.41845060685608,1.87363023778730,2.10021053665969;-2.47842156351806,-4.99324597672323,1.90651791078471;0.204283419234320,1.13357858075284,6.86677329350173;0.617793464714887,0.0559247730198438,6.10612769252045;-2.96417360323821,3.81704174520463,1.91580426879081;-5.20242439373799,0.476408999682868,0.991344090368801;5.32672360772571,2.06445068867013,2.47253796167180;-2.69346338350278,-5.85030685131976,2.37470982965291;0.218591936197924,1.32878562119851,7.84743096960627;-3.26247612265381,-4.91985596911837,1.29018005288354;1.20197539596291,-0.580347098756520,6.61000225666269;-3.37592827171665,4.67443410026770,2.22457026832058;-6.14584567995603,0.584770610127789,1.30473528056482;6.23299868743130,2.22581272192086,2.86321400922248;-2.89467385558292,-6.70726873056154,2.84918921113506;0.209717491582492,1.53184472980862,8.82655723451965;5.28440710514136,1.17097130314088,2.91964410258613;0.576278852478300,2.24513662818586,7.66753960818188;-4.04653068178957,-4.84646596151352,0.673842194982372;1.72465217643194,-1.18852390077965,7.20743841278270;-3.76500316313530,5.53715317053961,2.54758193166533;-7.08499357301506,0.666849489054822,1.63829830698843;7.13773556686645,2.38268963160310,3.25924533904186;-3.08850223839479,-7.56458389242937,3.32609724340970;0.176682672356017,1.77636612069925,9.79563823738556;5.70892694024996,0.283366758099107,3.09836478191717];
SizeA = size(A,1);
x = A(:, 1);
y = A(:, 2);
z = A(:, 3);
figure
plot(x, y, '.', 'color', 'k', 'Markersize', 12,'MarkerEdgeColor','w');
for ii = 1:SizeA
t = text(x(ii),y(ii),num2str(ii));
t.Color = 'k';
end
title('3D to 2D Mapping XY Plane: Index Numbers');
xlabel('x')
ylabel('y')
%%
figure
plot(x, z, '.', 'color', 'k', 'Markersize', 12,'MarkerEdgeColor','w');
for ii = 1:SizeA
t = text(x(ii),z(ii),num2str(ii));
t.Color = 'k';
end
title('3D to 2D Mapping XZ Plane: Index Numbers');
xlabel('x')
ylabel('z')
%%
dist1a = nan(numel(x));
proximity = 1.000;
save_criteria = 3;
save_criteria2 = 2;
for i = 1:SizeA
for j = 1:(i-1)
dist1a(i,j) = sqrt((x(i)-x(j)).^2 + (y(i)-y(j)).^2 + (z(i)-z(j)).^2);
dist1a(j,i) = dist1a(i,j);
end
end
i2keep = find(sum((dist1a - proximity <= eps('single')), 2) >= save_criteria);
i2keep2 = find(sum((dist1a - proximity <= eps('single')), 2) >= save_criteria2);
keep_x1 = x(i2keep);
keep_y1 = y(i2keep);
keep_z1 = z(i2keep);
B = 1:SizeA;
index = ismember(B,i2keep2);
%%
col = 1;
[master_index,~] = find(~index');
master_index_size = size(master_index,1);
indx = zeros(master_index_size,1);
for i = 1:master_index_size
while master_index(i,end) ~= 1
col = col+1;
indx = master_index(i,col-1);
C = A(indx,:);
master_index(i,col) = knnsearch(A(1:indx-1,:),C);
if master_index(i,col) == 1
break
end
end
col = 1;
end
%master_index = flip(master_index,2);
%%
x_c = 0;
y_c = 0;
z_c = 0;
Center_Root = [x_c, y_c, z_c];
[x_dom,y_dom,z_dom] = sphere(80); % Create Sphere
x_dom = x_dom(41:end,:); % Keep top 41 x points
y_dom = y_dom(41:end,:); % Keep top 41 y points
z_dom = z_dom(41:end,:); % Keep top 41 z points
hemisphere_radius = 80;
figure;
Hemi_sf = surf(hemisphere_radius.*x_dom,hemisphere_radius.*y_dom,hemisphere_radius.*z_dom, 'FaceColor','#4DBEEE','EdgeColor', 'none');
alpha 0.2
hold on
radii_plane = 80;
center_plane = [x_c, y_c]; % center point of circular plane
viscircles(center_plane, radii_plane, 'color', '#77AC30');
scale = 1;
s = 1*scale;
used_index = [];
h_SurfMatrix = [];
for i = 1:size(master_index,1)
branch = master_index(i,:);
branch(branch == 0) = [];
h = cell(numel(branch),1);
for j = 1:numel(branch)
if ismember(branch(1,j), i2keep)
s = s/2^(-1/3);
end
if ismember(branch(1,j),used_index)
h{j,:} = [];
continue
end
[x_loc,y_loc,z_loc, spheresXYZ{i,j}] = createspheres(A(branch(1,j),1),A(branch(1,j),2),A(branch(1,j),3), s);
h{j,:} = surf(x_loc+A(branch(1,j),1), y_loc+A(branch(1,j),2), z_loc+A(branch(1,j),3), 'FaceColor', 'k');
h = h(~any(cellfun('isempty', h), 2), :);
used_index = [used_index;branch(1,j)];
end
h_SurfMatrix = [h_SurfMatrix;h];
s = 1*scale;
end
%%
% Extract Surface XYZ Data
for i = 1:SizeA
Dx{i} = h_SurfMatrix{i}.XData;
Dy{i} = h_SurfMatrix{i}.YData;
Dz{i} = h_SurfMatrix{i}.ZData;
end
%%
% Concatenate Surface XYZ Data
Dx = cell2mat(Dx);
Dy = cell2mat(Dy);
Dz = cell2mat(Dz);
%%
function [X,Y,Z,spheresXYZ] = createspheres(spherex, spherey, spherez, s)
[x, y, z] = sphere(11);
X = (s*x)+spherex;
Y = (s*y)+spherey;
Z = (s*z)+spherez;
spheresXYZ = [X,Y,Z];
end

Connectez-vous pour commenter.

Réponse acceptée

Akira Agata
Akira Agata le 5 Jan 2021
How about the following solution?
test = sum(Index_matrix==0, 2);
[~, order] = sort(test);
Index_matrix = Index_matrix(order,:);
The result is like:
>> Index_matrix
Index_matrix =
49 40 33 27 22 17 12 7 4 2 1
42 33 27 22 17 12 7 4 2 1 0
44 35 28 22 17 12 7 4 2 1 0
47 38 31 25 20 15 10 5 2 1 0
48 39 32 26 21 16 11 6 3 1 0
50 41 31 25 20 15 10 5 2 1 0
43 34 26 21 16 11 6 3 1 0 0
45 36 29 23 18 13 8 2 1 0 0
46 37 30 24 19 14 9 3 1 0 0

Plus de réponses (1)

KSSV
KSSV le 5 Jan 2021
idx = [42 33 27 22 17 12 7 4 2 1 0
43 34 26 21 16 11 6 3 1 0 0
44 35 28 22 17 12 7 4 2 1 0
45 36 29 23 18 13 8 2 1 0 0
46 37 30 24 19 14 9 3 1 0 0
47 38 31 25 20 15 10 5 2 1 0
48 39 32 26 21 16 11 6 3 1 0
49 40 33 27 22 17 12 7 4 2 1
50 41 31 25 20 15 10 5 2 1 0];
id =sum(idx==0,2) ; % get the sum of zeros
[val,id1] = sort(id,'descend') ;
iwant = idx(id1,:)

Catégories

En savoir plus sur Data Distribution Plots dans Help Center et File Exchange

Produits


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by