Index exceeds matrix dimensions & Dimensions of matrices being concatenated are not consistent.
1 次查看(过去 30 天)
显示 更早的评论
hello everybody,
i am sharing my code and its giving some errors, i hope any of you guys will try to help me. here are 2 .m files. one is RM.m, in this file i am calling a function Train_Test.
//RM.m
// ind_pin.mat fill consist of 2 sub matrix, "fea" and "gnd", "fea" is 200x21025 and "gnd" is 1x21025.
clear;
clc
load ind_pin.mat
for kk=1:3
x = sprintf('iteration = %d',kk);
disp(x);
for numTrain=2:4
for M=5:2:7
L=20;
[feaTrain,gndTrain,feaTest,gndTest]=Train_Test(fea,gnd,numTrain);
[eigvector_PCA, eigvalue_PCA] = PCA(feaTrain,0);
[eigvector_Select] = RandomSelect(eigvector_PCA, 140);
pca_feaTrain = feaTrain*eigvector_Select;
pca_feaTest = feaTest*eigvector_Select;
acc=Accury(pca_feaTrain,gndTrain,pca_feaTest,gndTest,M,L);
A = sprintf('Accuracy = %6.3f',acc);
disp([A '%']);
end
end
end
/Train_Test.m
//function
function [feaTrain,gndTrain,feaTest,gndTest]=Train_Test(fea,gnd,numTrain)
X = sort(gnd);
temp = diff([X;max(X)+1]);
count = diff(find([1;temp]));
y = [X(find(temp)) count];
clear X temp count;
[nClass, tmp] = size(y);
m = 1;
for i = 1 : nClass
cSampleIdx = find(gnd == y(i,1));
R = randperm(y(i,2));
for j = 1 : numTrain
feaTrain((i-1)*numTrain+j,:) = fea(cSampleIdx(R(j)),:);
gndTrain((i-1)*numTrain+j) = gnd(cSampleIdx(R(j)));
end
for j = numTrain+1 : y(i,2)
feaTest(m,:) = fea(cSampleIdx(R(j)),:);
gndTest(m) = gnd(cSampleIdx(R(j)));
m = m+1;
end
end
gndTrain = gndTrain';
gndTest = gndTest';
clear R i j m cSampleIdx;
when i run it gives me following errors.
"Error using vertcat
Dimensions of matrices being concatenated are not consistent.
Error in Train_Test (line 4)
temp = diff([X;max(X)+1]);
Error in RMob (line 25)
[feaTrain,gndTrain,feaTest,gndTest]=Train_Test(fea,gnd,numTrain);
Index exceeds matrix dimensions.
Error in Train_Test (line 15)
feaTrain((i-1)*numTrain+j,:) = fea(cSampleIdx(R(j)),:);
Error in RMob (line 25)
[feaTrain,gndTrain,feaTest,gndTest]=Train_Test(fea,gnd,numTrain);
0 个评论
采纳的回答
Walter Roberson
2016-4-6
You have
X = sort(gnd);
temp = diff([X;max(X)+1]);
Your gnd is a row vector, so sort() of that will be a row vector, so X is a row vector. max(X) applied to a row vector is a scalar, and adding 1 to a scalar gives a scalar, so max(X)+1 is a scalar. X is still a row vector. So you have [row_vector; scalar] which tries to place the scalar "underneath" (on the next row) from the row vector. But the scalar does not have the same number of columns as the row vector does, so it fails.
You need to decide whether your [X;max(X)+1] needs to become a row vector or a column vector and make the appropriate change to the code.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!