sorting a matrix of 4 rows by the fourth element and outputting of combinations that make the 4th row element

조회 수: 1(최근 30일)
Leo Quintanar
Leo Quintanar 2021년 7월 22일
댓글: Leo Quintanar 2021년 7월 22일
My project so far has been to create a vector of numbers:
%x: a vector of numbers
x = 1:4;
and then create a 3 column matrix of the all the possible combinations of the vector x
%n: number of elements to pick (with repetetition) from x. n must be less than or equal to numel(x)
n = 3;
assert(n < numel(x), 'n can''t be greater than numel(x)')
combs = cell(1, n);
[combs{:}] = ndgrid(x);
combs = reshape(cat(n+1, combs{:}), [], n); %a numel(x)^n X n matrix of combinations
and add a 4th column to the matrix which is the energy of the the 3 columns which is equal
length = length(combs);
for x=1:length
combs(x,4) = combs(x, 1)^2+combs(x, 2)^2+combs(x, 3)^2;
E(x,:) = combs(x,4);
end
This produces a matrix similiar to the picture below:
This code works great but I want to re oragnize the rows according to the energy (4th) column to look like this:
So the rows will start with 4th column is 3 and then the next three are 6, and so fourth. I'm not sure how to do this, and would appreciate any help. Just a FYI I plan to change the vector from 4 to 14, this simulation is for Griffth's Quantum mechanics problem 4.2.B, I already know the answer but I figured it would be a fun to see the matrix blow up as well as practice coding. I remember doing something similiar in Java but its been a long time ago.
Thank you,
-Leo Q

채택된 답변

KSSV
KSSV 2021년 7월 22일
NOTE: Never use a function name as the variable. You have used length as variable, this is problematic.
clc; clear all ;
%x: a vector of numbers
x = 1:4;
%n: number of elements to pick (with repetetition) from x. n must be less than or equal to numel(x)
n = 3;
assert(n < numel(x), 'n can''t be greater than numel(x)')
combs = cell(1, n);
[combs{:}] = ndgrid(x);
combs = reshape(cat(n+1, combs{:}), [], n); %a numel(x)^n X n matrix of combinations
thelength = length(combs);
for x=1:thelength
combs(x,4) = combs(x, 1)^2+combs(x, 2)^2+combs(x, 3)^2;
E(x,:) = combs(x,4);
end
%% Arrange combs
v = combs(:,end) ;
[c,ia,ib] = unique(v) ;
iwant = cell(length(c),1) ;
for i = 1:length(c)
iwant{i} = combs(ib==i,:) ;
end
celldisp(iwant)
iwant{1} = 1 1 1 3 iwant{2} = 2 1 1 6 1 2 1 6 1 1 2 6 iwant{3} = 2 2 1 9 2 1 2 9 1 2 2 9 iwant{4} = 3 1 1 11 1 3 1 11 1 1 3 11 iwant{5} = 2 2 2 12 iwant{6} = 3 2 1 14 2 3 1 14 3 1 2 14 1 3 2 14 2 1 3 14 1 2 3 14 iwant{7} = 3 2 2 17 2 3 2 17 2 2 3 17 iwant{8} = 4 1 1 18 1 4 1 18 1 1 4 18 iwant{9} = 3 3 1 19 3 1 3 19 1 3 3 19 iwant{10} = 4 2 1 21 2 4 1 21 4 1 2 21 1 4 2 21 2 1 4 21 1 2 4 21 iwant{11} = 3 3 2 22 3 2 3 22 2 3 3 22 iwant{12} = 4 2 2 24 2 4 2 24 2 2 4 24 iwant{13} = 4 3 1 26 3 4 1 26 4 1 3 26 1 4 3 26 3 1 4 26 1 3 4 26 iwant{14} = 3 3 3 27 iwant{15} = 4 3 2 29 3 4 2 29 4 2 3 29 2 4 3 29 3 2 4 29 2 3 4 29 iwant{16} = 4 4 1 33 4 1 4 33 1 4 4 33 iwant{17} = 4 3 3 34 3 4 3 34 3 3 4 34 iwant{18} = 4 4 2 36 4 2 4 36 2 4 4 36 iwant{19} = 4 4 3 41 4 3 4 41 3 4 4 41 iwant{20} = 4 4 4 48
  댓글 수: 1
Leo Quintanar
Leo Quintanar 2021년 7월 22일
Thanks that worked perfectly, also thank you for your comment about my stupidity regrading my varaible names.

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by