Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 1-by-2.
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I found some trouble with my code, somebody help me please. I really need your help, thankyou!
clc; clear all;
A=xlsread('sne7-final.xlsx','cv','A1:G64');
B=xlsread('sne7-final.xlsx','cv','A129:G640');
% Time=[A;B];
% data=Time(:,1:7);
% target=Time(:,8);
C=xlsread('sne7-final.xlsx','cv','H1:H64');
D=xlsread('sne7-final.xlsx','cv','H129:H640');
% Target=[C;D];
% contohData = xlsread('All Entropy 25 Blok');
contohData = [A;B];
% hasilContohData = xlsread('Hasil Entropy 25 Blok CV10');
hasilContohData = [C;D];
dataBaru = xlsread('sne7-final.xlsx','cv','A65:G128');
%Tentukan parameter k, yaitu banyak jumlah titik tetangga yang diperhitungkan
%Karena ada 4 data pada contoh awal, maka nilai K adalah 1 / 2 / 3 / 4
%Diasumsikan dalam kasus ini, nilai K adalah 3, brarti hanya 3 titik tetangga terdekat yang diperhitungkan dalam setiap perhitungan
k = 1;
%* Lakukan perhitungan menggunakan metode Fuzzy k-NN (k-Nearest Neighbors)
%Penjelasan tentang fungsi ini akan dijelaskan pada perhitungan dibawah ini (poin 1 - 3)
[hasilData,jarakKeMasing2Hasil] = FuzzyKNN(contohData, hasilContohData, dataBaru, k);
disp('Hasil Perhitungan dengan metode Fuzzy k-NN (k-Nearest Neighbors)')
disp('Thorax , Region 1 , Region 2 , Region 3 , Jarak ke hasil abnormal , Jarak ke hasil Normal, kesimpulan')
for i = 1:size(hasilData, 1)
%4. Lakukan konversi nilai hasil kembali seperti semula
%Jika nlai hasil adalah 1, maka termasuk dalam kelompok hasil jual
%Laris (abnormal)
%Jika nlai hasil adalah 2, maka termasuk dalam kelompok hasil jual
%Tidak Laris (normal)
if hasilData(i,1) == 1
status = '1';
elseif hasilData(i,1) == 2
status = '2';
end
disp(['Thorax ' , num2str(i) , ', ', ...
num2str(dataBaru(i,1)), blanks(10 - length(num2str(dataBaru(i,1)))), ', ', ...
num2str(dataBaru(i,2)), blanks(22 - length(num2str(dataBaru(i,2)))), ', ', ...
num2str(dataBaru(i,3)), blanks(3 - length(num2str(dataBaru(i,3)))), ', ', ...
num2str(round(jarakKeMasing2Hasil(i,1),2)), blanks(25 - length(num2str(round(jarakKeMasing2Hasil(i,1),2)))), ', ', ...
num2str(round(jarakKeMasing2Hasil(i,2),2)), blanks(31 - length(num2str(round(jarakKeMasing2Hasil(i,2),2)))), ', ', ...
char(status)])
x=status;
sheet=1;
my_cell = sprintf('A%s',num2str(i));
xlswrite('Hitung Akurasi SNE 7 Blok CV1 1',x,sheet,my_cell);
end
filename4='Hitung Akurasi SNE 7 Blok CV1 1.xls'; %hasil klasifikasi
filename5=xlsread('sne7-final.xlsx','cv','H65:H128') %hasil asli dari database
predict=xlsread(filename4); %inisiasi
true=xlsread(filename5); %inisiasi
C=confusionmat(true,predict)
confusionchart(C) % buat nampilin chart true false dkk
[m,n]=find(true==predict);
akurasi=sum(n)/64*100 %rumus akurasi
this is the function
function [hasilDataBaru,jarakKeMasing2Hasil] = FuzzyKNN(contohData, hasilContohData, dataBaru, k, fuzzy)
if nargin<7
fuzzy = true;
end
jumlahContohData = size(contohData,1);
jumlahDataBaru = size(dataBaru,1);
%1. Tentukan faktor skala dalam pembobotan fuzzy
%Dalam kasus ini bernilai 2 karena hanya ada 2 nilai hasil, yaitu Laris dan Tidak Laris
m = 2;
%2. Lakukan konversi nilai hasil contoh data menjadi 2 nilai biner
%Untuk hasil jual Laris, maka akan dikonversi menjadi 1 0
%Untuk hasil jual Tidak Laris, maka akan dikonversi menjadi 0 1
maksHasilContohData = max(hasilContohData);
temp = zeros(length(hasilContohData),maksHasilContohData);
for i=1:jumlahContohData
temp(i,:) = [zeros(1, hasilContohData(i)-1) 1 zeros(1,maksHasilContohData - hasilContohData(i))];
end
hasilContohData = temp;
%Untuk menyimpan hasil jual untuk setiap data baru
hasilDataBaru = zeros(jumlahDataBaru, length(k));
%Untuk menyimpan jarak ke masing-masing hasil jual (Laris dan Tidak Laris)
jarakKeMasing2Hasil = zeros(jumlahDataBaru, maksHasilContohData, length(k));
%3. Lakukan perhitungan pada masing-masing data baru (poin 3a - 3g)
for i=1:jumlahDataBaru
%3a. Hitung jarak dari data baru pada masing-masing data contoh
%Perhitungan jarak menggunakan rumus Euclidean, yaitu akar dari (jumlah dari (kuadrat dari (dataBaru - contohData)))
%Untuk efisiensi, proses akar memang tidak dilakukan agar tidak perlu lagi mengkuadratkan pada proses selanjutnya
jarak = (repmat(dataBaru(i,:), jumlahContohData,1) - contohData).^2;
jarak = sum(jarak,2)';
%3b. Lakukan pengurutan jarak dari yang paling dekat ke jarak yang paling jauh
[~, idxTerurut] = sort(jarak);
%3c. Contoh data terpilih adalah contoh data dengan jarak yang paling minimal sebanyak parameter k
idxContohDataTerpilih = idxTerurut(1:k);
bobot = ones(1,length(idxContohDataTerpilih));
if fuzzy,
%3d. Lakukan perhitungan bobot menggunakan metode fuzzy
%Normalnya, nilai bobot dihitung dengan menggunakan rumus:
%bobot = jarak(idxContohDataTerpilih).^(-2/(m-1));
%Tetapi karena nilai jarak diatas tidak dilakukan proses akar,
%Maka nilai bobotnya dihitung dengan rumus:
%bobot = sqrt(jarak(idxContohDataTerpilih)).^(-2/(m-1));
%Dengan menghilangkan proses akar dan kuadrat yang terjadi bersamaan,
%maka bobot dapat dihitung dengan rumus:
%bobot = jarak(idxContohDataTerpilih).^(-1/(m-1));
bobot = jarak(idxContohDataTerpilih).^(-1/(m-1));
%3e. Jika terdapat nilai bobot yang mendekati tak terhingga (infinity),
%maka nilai bobot ini akan dikembalikan menjadi angka 1
if max(isinf(bobot))
warning(['Terdapat bobot dengan nilai tak terhingga: ' num2str(i) '. Sehingga nilai bobot dikembalikan menjadi 1.']);
bobot(isinf(bobot)) = 1;
end
end
%3f. Lakukan perkalian nilai bobot dengan 2 nilai biner hasil konversi dari nilai hasil pada contoh data
%Kemudian lakukan normalisasi dengan cara membagi bobot tersebut dengan semua jumlah bobot yang ada
bobotPerMasing2Hasil = bobot * hasilContohData(idxContohDataTerpilih,:) / (sum(bobot)) ;
jarakKeMasing2Hasil(i,:) = bobotPerMasing2Hasil;
%3g. Tentukan nilai bobot yang paling tinggi,
%Kemudian catat indeks bobot tersebut sebagai jawaban akhir
[~, idxMaks] = max(bobotPerMasing2Hasil');
hasilDataBaru(i) = idxMaks;
end
this is the eror
Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 1-by-2.
Error in FuzzyKNN (line 19)
temp(i,:) = [zeros(1, hasilContohData(i)-1) 1 zeros(1,maksHasilContohData - hasilContohData(i))];
Error in main (line 28)
[hasilData,jarakKeMasing2Hasil] = FuzzyKNN(contohData, hasilContohData, dataBaru, k);
2 Commenti
Tommy
il 18 Apr 2020
The following code:
[zeros(1, hasilContohData(i)-1) 1 zeros(1,maksHasilContohData - hasilContohData(i))]
will only produce a vector with length equal to maksHasilContohData under certain circumstances, one of which is that hasilContohData(i) is positive. To simplify, consider the function:
function v = f(a,b)
v = [zeros(1, a-1) 1 zeros(1,b-a)];
end
This works as expected:
>> f(1,2)
ans =
1 0
But this does not:
>> f(0,2)
ans =
1 0 0 % length is 3, which is problematic as b was 2
Can you always be sure, in your code, that hasilContohData(i) is positive. Based on the error message, I would guess that maksHasilContohData is 1 and somewhere within hasilContohData there is a 0.
Risposta accettata
KALYAN ACHARJYA
il 18 Apr 2020
Seems you have to consider two loops, as temp is now 1 dimentional vector
Look this way, to assign both indexing (spatial position) of temp data
for i=1:jumlahContohData
for j=1:jumlahDataBaru
temp(i,j) = [zeros(1, hasilContohData(i)-1) 1 zeros(1,maksHasilContohData - hasilContohData(i))];
end
end
Please note on the length of arrays.
0 Commenti
Più risposte (1)
Walter Roberson
il 18 Apr 2020
maksHasilContohData = max(hasilContohData);
temp = zeros(length(hasilContohData),maksHasilContohData);
for i=1:jumlahContohData
temp(i,:) = [zeros(1, hasilContohData(i)-1) 1 zeros(1,maksHasilContohData - hasilContohData(i))];
end
Suppose that hasilContohData is a vector with at least two elements, and the maximum element of the vector is 1. In particular, suppose the vector is [0 1]. Then the maximum, maksHasilContohData, would be 1. length(hasilContohData) is 2, and maksHasilContohData is 1, so zeros(2,1) is assigned to temp.
Now loop on i. starting with i = 1. hasilControhData(1) is 0, so the first part of the [] is zeros(1, 0-1) which turns out to be the same as zeros(1,0) which is empty. The second part is the scalar 1, which has one element. The third part is zeros(1, 1-0) which is zeros(1,1) which is 1 element. Putting the three together you get [empty, 1, 0] which would be [1 0], which is 1 x 2. But the destination, temp(1,:) is only 1 x 1, so you get an error.
You would encounter this error any time your hasilContohData is an array that contains a mix of 0 and 1 values.
More generally, the same kind of error would occur any time you have 0's in the vector, including the case where the vector is all 0. zeros(1,-1) does not somehow create a negative space to balance out the 1 + maxHasilContohData length you get on the right when the right has been declared to have maxHasilContohData columns -- you have an off-by-1 error in your size calculation when you have zeros in the input.
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!