0

I have tried this code for my assignments, but I am getting error of type

??? Subscript indices must either be real positive integers or logicals.

This is my code:

for i = 5:200
    eigvecm = eigvecm(:, end:-1:end-(int8(i)-1));
end

Please point me out how to get this done?

Dan
  • 45,079
  • 17
  • 88
  • 157
  • Wow! What are you doing? Using "i" as iterator and then changing "eigvecm" in the loop itself and then using "end" as the indexing. – Divakar Mar 04 '14 at 08:10
  • in **eigvecm=eigvecm(:,`end:-1:end-(int8(i)-1)`)** what do you want to do here? – Ankush Mar 04 '14 at 08:11
  • I am trying to get maximum of 'i' column from eigvecm matrix, It works fine if I use number like 5 etc,..but not working when i use 'i' as variable. –  Mar 04 '14 at 08:14
  • 2
    here what exactly happen is, it will give you out the i number of eigenvector corresponding to first ith maximum eigenvalue. –  Mar 04 '14 at 08:17
  • Also see [this question](http://stackoverflow.com/questions/20054047/subscript-indices-must-either-be-real-positive-integers-or-logicals-generic-sol) for a [generic approach](http://stackoverflow.com/a/20054048/983722) to deal with this error. – Dennis Jaheruddin Mar 10 '14 at 10:30

1 Answers1

1

It sounds very much like end-(int8(i)-1) ends up reaching zero or below. Check what is the value of i when you get the error and compare this to how many columns eigvecm has.

BTW if you want the eigen vecotr corresponding to the ith largest eigen value how about this:

[vec, val] = eig(M);
[~, ind] = sort(diag(val), 'descend');

ind(i) is the column number for the ith largest eigen value. So to find the corresponding eigen vector:

vec_i = vec(:, ind(i));
Dan
  • 45,079
  • 17
  • 88
  • 157
  • thank you for your amazing answer, I got the concept. –  Mar 04 '14 at 08:29
  • @cybertrone so what was wrong though? Was `i` getting larger than the number of columns of `eigvecm`? – Dan Mar 04 '14 at 08:34
  • yes , you were right,I was applying this code for pca. –  Mar 04 '14 at 08:35
  • 1
    You might consider to use the SVD functions to do PCA. Singular values usually are returned sorted from largest to smallest. – Lutz Lehmann Mar 11 '14 at 21:41