[37] | 1 | function [oldvariable,sqrList] = expandforindex(sqrList,candidates,nonconvindex); |
---|
| 2 | %EXPANDFORINDEX Internal function for organizing nonlinear variables |
---|
| 3 | |
---|
| 4 | % Author Johan Löfberg |
---|
| 5 | % $Id: expandforindex.m,v 1.4 2004/08/06 09:02:52 johanl Exp $ |
---|
| 6 | |
---|
| 7 | |
---|
| 8 | % Expand the incoming variables |
---|
| 9 | |
---|
| 10 | if 1 |
---|
| 11 | z = sqrList(:,1); |
---|
| 12 | |
---|
| 13 | x1_list = find(z==candidates(2)); |
---|
| 14 | if candidates(2)==candidates(1) |
---|
| 15 | x2_list = x1_list; |
---|
| 16 | else |
---|
| 17 | x2_list = find(z==candidates(1)); |
---|
| 18 | end |
---|
| 19 | |
---|
| 20 | temp = []; |
---|
| 21 | if isempty(x1_list) |
---|
| 22 | temp = candidates(2); |
---|
| 23 | else |
---|
| 24 | temp = sqrList(x1_list,2:end); |
---|
| 25 | end |
---|
| 26 | if isempty(x2_list) |
---|
| 27 | temp = [temp candidates(1)]; |
---|
| 28 | else |
---|
| 29 | temp = [temp sqrList(x2_list,2:end)]; |
---|
| 30 | end |
---|
| 31 | temp = sort(temp(find(any(temp,1)))); |
---|
| 32 | |
---|
| 33 | oldsqrList = sqrList; |
---|
| 34 | if length(temp)>size(sqrList,2)-1 |
---|
| 35 | % This one cannot already exist |
---|
| 36 | index = []; |
---|
| 37 | |
---|
| 38 | %sqrList = [sqrList zeros(size(sqrList,1),1+size(sqrList,2)-length(temp));nonconvindex temp(end:-1:1)]; |
---|
| 39 | sqrList = [sqrList zeros(size(sqrList,1),1+length(temp)-size(sqrList,2));nonconvindex temp(end:-1:1)]; |
---|
| 40 | else |
---|
| 41 | % sqrList = [sqrList;nonconvindex temp(end:-1:1) zeros(1,size(sqrList,2)-length(temp)-1)]; |
---|
| 42 | % searchfor = sqrList(end,2:end); |
---|
| 43 | |
---|
| 44 | searchfor = [temp(end:-1:1) zeros(1,size(sqrList,2)-length(temp)-1)]; |
---|
| 45 | searchin = sqrList(:,2:end); |
---|
| 46 | |
---|
| 47 | % Simple Hash |
---|
| 48 | key = sum(searchfor); |
---|
| 49 | tbl = sum(searchin,2); |
---|
| 50 | |
---|
| 51 | possible = find(tbl==key); |
---|
| 52 | |
---|
| 53 | index = findrows(searchin(possible,:),searchfor); |
---|
| 54 | if isempty(index) |
---|
| 55 | sqrList = [sqrList;nonconvindex temp(end:-1:1) zeros(1,size(sqrList,2)-length(temp)-1)]; |
---|
| 56 | else |
---|
| 57 | index = possible(index); |
---|
| 58 | end |
---|
| 59 | end |
---|
| 60 | |
---|
| 61 | %searchfor = sqrList(end,2:end); |
---|
| 62 | %index = findrows(sqrList(1:end-1,2:end),searchfor); |
---|
| 63 | |
---|
| 64 | if length(index)>1 |
---|
| 65 | index |
---|
| 66 | end |
---|
| 67 | |
---|
| 68 | if ~isempty(index) |
---|
| 69 | oldvariable = sqrList(index,1); |
---|
| 70 | sqrList = oldsqrList; |
---|
| 71 | else |
---|
| 72 | oldvariable = []; |
---|
| 73 | end |
---|
| 74 | |
---|
| 75 | |
---|
| 76 | |
---|
| 77 | else |
---|
| 78 | |
---|
| 79 | |
---|
| 80 | % Expand the list to begin with |
---|
| 81 | if isempty(sqrList) |
---|
| 82 | sqrList = [nonconvindex candidates]; |
---|
| 83 | else |
---|
| 84 | sqrList = [sqrList; nonconvindex candidates(2) candidates(1) zeros(1,size(sqrList,2)-3)]; |
---|
| 85 | end |
---|
| 86 | bottom = size(sqrList,1); |
---|
| 87 | |
---|
| 88 | %%FIXz = sqrList(1:bottom,1); |
---|
| 89 | z = sqrList(:,1); |
---|
| 90 | |
---|
| 91 | x1_list = find(z==candidates(2)); |
---|
| 92 | if candidates(2)==candidates(1) |
---|
| 93 | x2_list = x1_list; |
---|
| 94 | else |
---|
| 95 | x2_list = find(z==candidates(1)); |
---|
| 96 | end |
---|
| 97 | |
---|
| 98 | temp = []; |
---|
| 99 | if isempty(x1_list) |
---|
| 100 | temp = sqrList(bottom,2); |
---|
| 101 | else |
---|
| 102 | temp = sqrList(x1_list,2:end); |
---|
| 103 | end |
---|
| 104 | if isempty(x2_list) |
---|
| 105 | temp = [temp sqrList(bottom,3)]; |
---|
| 106 | else |
---|
| 107 | temp = [temp sqrList(x2_list,2:end)]; |
---|
| 108 | end |
---|
| 109 | temp = sort(temp(find(any(temp,1)))); |
---|
| 110 | sqrList(bottom,2:2+length(temp)-1)=temp(end:-1:1);%fliplr(sort(temp)); |
---|
| 111 | |
---|
| 112 | bottom = bottom+1; |
---|
| 113 | |
---|
| 114 | searchfor = sqrList(end,2:end); |
---|
| 115 | index = findrows(sqrList(1:end-1,2:end),searchfor); |
---|
| 116 | |
---|
| 117 | if ~isempty(index) |
---|
| 118 | oldvariable = sqrList(index,1); |
---|
| 119 | sqrList = sqrList(1:end-1,:); |
---|
| 120 | else |
---|
| 121 | oldvariable = []; |
---|
| 122 | end |
---|
| 123 | |
---|
| 124 | end |
---|