bughousechesslife

Untitled

Mar 4th, 2023
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 4.93 KB | None | 0 0
  1. clear
  2. clc
  3. close all
  4.  
  5. exampleMatrix = [1, 3, -1 0, 4
  6.     4, 1, 7, 11, 5
  7.     0, 4, -4, -4, 4
  8.     2, 0 4 6, 2 ];
  9.  
  10. [R_example,V_example]=rref(exampleMatrix)
  11.  
  12. % reconstruct column in ExampleMatrix from a combination of the other
  13. % columns following example
  14. % https://math.stackexchange.com/questions/1624238/reduced-row-echelon-form-and-linear-independence
  15.  
  16. % Entries in V contain pivot columns. Entry 3 does not appear in V, so it
  17. % is a nonpivot column. Loop through entries in column 3 of RREF(ExampleMatrix)
  18. % and use each as a coefficient to multiply the respective column in
  19. % ExampleMatrix by in order to obtain a linear combination that sums to the
  20. % column3 of
  21.  
  22. originalColumn = zeros( [length(exampleMatrix(:,3)) 1] );
  23. for i=1:length(R_example(:,3))
  24.     originalColumn = R_example(i,3)*exampleMatrix(:,i) + originalColumn;
  25. end
  26. originalColumn3ExampleMatrix=originalColumn
  27. actualColumn3ExampleMatrix=(exampleMatrix(:,3))
  28.  
  29. % part 2: try the same thing with the crazy matrix
  30.  
  31. A = [0  0   0   -2  -1  -1  -1  -1  -1  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
  32. 0   0   0   0   -1  0   0   0   0   -1  -1  -1  -1  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   1   1
  33. 0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   1   0   1   0   1   0   1   0   0   0
  34. 0   0   -1  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   1   0   0   1   2   1   1   0   1
  35. -1  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   1   0   0   0   1   0   0
  36. 0   -1  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   1   1   1   1   0   0   0   0   1   0
  37. 1   0   0   0   0   -1  0   0   0   -1  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
  38. 0   1   0   0   0   0   -1  0   -1  0   -1  0   -1  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
  39. 0   0   1   0   0   0   0   -1  0   0   0   -1  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
  40. 0   0   0   1   0   0   0   0   0   0   0   0   0   -1  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
  41. 0   0   0   0   1   0   0   0   0   0   0   0   0   0   -1  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
  42. 0   0   0   0   0   1   0   0   0   0   0   0   0   0   0   -1  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
  43. 0   0   0   0   0   0   1   0   0   0   0   0   0   0   0   0   -1  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
  44. 0   0   0   0   0   0   0   1   0   0   0   0   0   0   0   0   0   -1  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
  45. 0   0   0   0   0   0   0   0   1   0   0   0   0   0   0   0   0   0   -1  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
  46. 0   0   0   0   0   0   0   0   0   1   0   0   0   0   0   0   0   0   0   -1  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
  47. 0   0   0   0   0   0   0   0   0   0   1   0   0   0   0   0   0   0   0   0   -1  0   0   0   0   0   0   0   0   0   0   0   0   0   0
  48. 0   0   0   0   0   0   0   0   0   0   0   1   0   0   0   0   0   0   0   0   0   -1  0   0   0   0   0   0   0   0   0   0   0   0   0
  49. 0   0   0   0   0   0   0   0   0   0   0   0   1   0   0   0   0   0   0   0   0   0   -1  0   0   0   0   0   0   0   0   0   0   0   0
  50. 0   0   0   0   0   0   0   0   0   0   0   0   0   1   0   0   0   0   0   0   0   0   0   -1  0   0   0   0   0   0   0   0   0   0   0
  51. 0   0   0   0   0   0   0   0   0   0   0   0   0   0   1   0   0   0   0   0   0   0   0   0   -1  0   0   0   0   0   0   0   0   0   0
  52. 0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   1   0   0   0   0   0   0   0   0   0   -1  0   0   0   0   0   0   0   0   0
  53. 0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   1   0   0   0   0   0   0   0   0   0   -1  0   0   0   0   0   0   0   0
  54. 0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   1   0   0   0   0   0   0   0   0   0   -1  0   0   0   0   0   0   0
  55. 0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   1   0   0   0   0   0   0   0   0   0   -1  0   0   0   0   0   0
  56. 0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   1   0   0   0   0   0   0   0   0   0   -1  0   0   0   0   0
  57. 0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   1   0   0   0   0   0   0   0   0   0   -1  0   0   0   0
  58. 0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   1   0   0   0   0   0   0   0   0   0   -1  0   0   0
  59. 0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   1   0   0   0   0   0   0   0   0   0   -1  0   0
  60. 0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   1   0   0   0   0   0   0   0   0   0   -1  0
  61. 0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   1   0   0   0   0   0   0   0   0   0   -1];
  62.  
  63.  
  64. [R,V]=rref(A);
  65.  
  66. % reconstruct row in A from a combination of the others
  67.  
  68. % V contains the pivot columns.. need nonpivot columns, for now, let's just
  69. % take column 3 as an example
  70.  
  71. % loop through entries in column 3 and see if I can get back the original
  72. % column...
  73. my_npc_index = 34;
  74. originalColumnFromLinearCombination = zeros( [length(A(:,my_npc_index)) 1] );
  75. counter=1;
  76. for i=1:length(R(:,my_npc_index))
  77.     if (ismember(i,V))
  78.         originalColumnFromLinearCombination = R(counter,my_npc_index)*A(:,i) + originalColumnFromLinearCombination;
  79.         counter = counter+1;
  80.     end
  81. end
  82. originalColumnFromLinearCombination
  83. actualOriginalColumn = A(:,my_npc_index)
  84.  
  85. % SCRIPT OUTPUT
  86. % R_example =
  87. %
  88. %      1     0     2     3     1
  89. %      0     1    -1    -1     1
  90. %      0     0     0     0     0
  91. %      0     0     0     0     0
  92. %
  93. %
  94. % V_example =
  95. %
  96. %      1     2
  97. %
  98. %
  99. % originalColumn3ExampleMatrix =
  100. %
  101. %     -1
  102. %      7
  103. %     -4
  104. %      4
  105. %
  106. %
  107. % actualColumn3ExampleMatrix =
  108. %
  109. %     -1
  110. %      7
  111. %     -4
  112. %      4
  113. %
  114. %
  115. % originalColumnFromLinearCombination =
  116. %
  117. %      0
  118. %      1
  119. %      0
  120. %      0
  121. %      0
  122. %      1
  123. %      0
  124. %      0
  125. %      0
  126. %      0
  127. %      0
  128. %      0
  129. %      0
  130. %      0
  131. %      0
  132. %      0
  133. %      0
  134. %      0
  135. %      0
  136. %      0
  137. %      0
  138. %      0
  139. %      0
  140. %      0
  141. %      0
  142. %      0
  143. %      0
  144. %      0
  145. %      0
  146. %     -1
  147. %      0
  148. %
  149. %
  150. % actualOriginalColumn =
  151. %
  152. %      0
  153. %      1
  154. %      0
  155. %      0
  156. %      0
  157. %      1
  158. %      0
  159. %      0
  160. %      0
  161. %      0
  162. %      0
  163. %      0
  164. %      0
  165. %      0
  166. %      0
  167. %      0
  168. %      0
  169. %      0
  170. %      0
  171. %      0
  172. %      0
  173. %      0
  174. %      0
  175. %      0
  176. %      0
  177. %      0
  178. %      0
  179. %      0
  180. %      0
  181. %     -1
  182. %      0
Advertisement
Add Comment
Please, Sign In to add comment