### Translated by

このページのコンテンツは英語から自動翻訳されています。自動翻訳をオフにする場合は「<a class="turn_off_mt" href="#">ここ</a>」をクリックしてください。

## Using a 2 dimensional feature

G. Nardi

### G. Nardi (view profile)

さんによって質問されました 2019 年 1 月 18 日

### G. Nardi (view profile)

さんによって コメントされました 2019 年 1 月 21 日
Walter Roberson

### Walter Roberson (view profile)

さんの 回答が採用されました
I am current student and new to Matlab. For an assignment I was told that I was not using the 2 dimensional feature for a .m file that I loaded. How do I do this and how do I figure it out? Where are my mistakes?
partTwoData is a 10000x2 double.
My variables made are coming up as x1
numberOfClassOneSamples = size(classOne, 1);
numberOfClassTwoSamples = size(classTwo, 1);
%Generate a random partition of the data,
randomizedIndices = randsample(numberOfClassOneSamples, numberOfClassOneSamples);
%splitting each of the classes into 60% training and 40%
classOneTrainingIndices = randomizedIndices(1:6000);
classOneTestIndices= randomizedIndices(6001:10000);
%for classTwo
classTwoTrainingIndices = randomizedIndices(1:6000);
classTwoTestIndices= randomizedIndices(6001:10000);
classOneTrainingData = classOne(classOneTrainingIndices);
classOneTestData = classOne(classOneTestIndices);
classTwoTrainingData = classTwo(classTwoTrainingIndices);
classTwoTestData = classTwo(classTwoTestIndices);
numberOfTestSamples = 4000;
numberOfTrainingSamples = 6000;
%class one test samples
for i=1:numberOfTestSamples
for j=1:numberOfTrainingSamples
euclideanDistanceClassOne(j) = sqrt ( (classOneTestData(i) - classOneTrainingData(j))^2);
end
for j=1 : numberOfTrainingSamples
euclideanDistanceClassTwo(j) = sqrt( (classOneTestData(i) - classTwoTrainingData(j))^2);
end
if(min(euclideanDistanceClassOne) < min(euclideanDistanceClassTwo))
%classify as class one
predict(i) = 1;
else
predict(i) = 2;
end
end
correctClassOne = sum(predict==1);
incorrectClassOne = sum(predict==2);
%Class Two Test Samples, still j, because we are stepping through the training sample.
for i=1:numberOfTestSamples
for j=1:numberOfTrainingSamples
euclideanDistanceClassOne(j) = sqrt((classTwoTestData(i) - classOneTrainingData(j))^2);
end
for j=1:numberOfTrainingSamples
euclideanDistanceClassTwo(j) = sqrt((classTwoTestData(i) - classTwoTrainingData(j))^2);
end
if (min(euclideanDistanceClassOne) < min(euclideanDistanceClassTwo))
%classify as class one
predict(i)=1;
else
%classify as class two
predict(i)=2;
end
end
correctClassTwo = sum(predict == 2);
incorrectClassTwo = sum(predict == 1);
fprintf('Part Two:');
fprintf('Class One Correct Predictions = %d\n', correctClassOne);
fprintf('Class Two Correct Predictions = %d\n', correctClassTwo);
fprintf('Class One Incorrect Predictions = %d\n', incorrectClassOne);
fprintf('Class Two Incorrect Predictions = %d\n', incorrectClassTwo);
fprintf('Total Class One Incorrect Predictions = %d\n', correctClassOne + correctClassTwo);
fprintf('Total Class Two Incorrect Predictions = %d\n', incorrectClassOne + incorrectClassTwo);
fprintf('Total Prediction Accuracy = %f\n', (correctClassOne + correctClassTwo) / (2 * numberOfTestSamples));

#### 0 件のコメント

サインイン to comment.

R2018b

## 1 件の回答

### Walter Roberson (view profile)

2019 年 1 月 19 日
採用された回答

Every place that you index classTwoTestData(some_index) you need to change it to classTwoTestData(some_index,:) and likewise for similar variables.
You also need to adjust your euclidean distance calculations. A(index,:) - B(index,:) will be a vector, so you cannot use ^2 for it: you need to use .^2 . You also need to take the sum of the squared components.

G. Nardi

### G. Nardi (view profile)

2019 年 1 月 20 日
I am still a student and trying to learn. Nothing is really obvious.
I tried
%class one test samples
for i=1:numberOfTestSamples
for j=1:numberOfTrainingSamples
euclideanDistanceClassOne(j) = sqrt ( (classOneTestData(i,:) - classOneTrainingData(j,:)).^2);
end
But I get this error
Unable to perform assignment because the left and right sides have a different number of
elements.
Error in
euclideanDistanceClassOne(j) = sqrt ( (classOneTestData(i,:) -
classOneTrainingData(j,:)).^2);
Walter Roberson

### Walter Roberson (view profile)

2019 年 1 月 20 日
If you have a vector of values, such as
(classOneTestData(i,:) - classOneTrainingData(j,:)).^2
is a vector of values, then how would you find the total value?
G. Nardi

### G. Nardi (view profile)

2019 年 1 月 21 日
sum! got it thanks.

サインイン to comment.

Translated by