在matlab中,我得到了方阵的稀疏表示。我试图执行本征分析,计算它的本征值和特征向量n-1次,去掉它的时间,一行和一列。我的问题是如何执行行和列删除。我的代码:
A = textread('matrices.txt'); %nx3 matrix
for k=1:n% size of the matrix
display(k)
temp = A;
for index_i =1:length(temp)
if(temp(index_i, 1)== k | temp(index_i,2) == k)
temp(index_i,:) = [];
end
end
S = spconvert(temp); % sparse representation of the matrix
[a b] = eigs(S); % calculate the first six eigenvalues and eigenvectors
temp_vectors(:,k) = a(:,1);
temp_values(k) = b(1,1);
end在一行temp(index_i,:) =[]中,我遇到了索引问题;基本上您是对的,我做了一些不同的事情,但是您的实现更好:
for k=1:10000 % size of the matrix
temp = A;
counter = 1;
list = [];
display(k)
for index_i =1:length(temp)
if(temp(index_i, 1)== k | temp(index_i,2) == k)
list(counter) = index_i;
counter = counter + 1;
end
end
temp(list(:),:)= [];
size(temp)
S = spconvert(temp); % sparse representation of the matrix
[a b] = eigs(S); % calculate the first six eigenvalues and eigenvectors
%temp_vectors(:,k) = a(:,1);
temp_values(k) = b(1,1);
name = strcat('eigen_vectors\eigen_vector_', int2str(k) ,'.mat');
vec = a(:,1);
save(name, 'vec');结束
此外,出现了新的麻烦。这个问题存在于第n特征分析的计算中。为了分析我的矩阵,我必须删除第10,000行和第10,000列。但是,通过这样做,它计算出一个大小为9999的特征向量,因为它删除了第10000行/列。有什么办法克服这个问题吗?
发布于 2014-06-10 10:53:38
当您使用[]移除值时,您正在更改矩阵的大小。但是在您的内部循环中,您使index_i运行到temp的初始最大大小。因此,您可能会到达一个index_i大于当前temp大小的点,您将得到一个相应的错误。
与使用循环一次删除它们不同,您可以使用逻辑索引一步一步完成此操作:
temp(temp(:,1)==k|temp(:,2)==k,:)=[];https://stackoverflow.com/questions/24135932
复制相似问题