How to make two vectors the same size?

조회 수: 32 (최근 30일)
phdcomputer Eng
phdcomputer Eng 2019년 12월 10일
댓글: phdcomputer Eng 2019년 12월 11일
I wrote these codes, I splitted the dataset(colon attached) into two parts (data1 and data2) based on the last column(1 or else)and make the columns equal to each others.
clc;
clear;
close all;
tic
load colon.mat
data=colon;
[n,m]=size(data);
d=10;
l=1;
t=1;
data1=[];
data2=[];
for i=1:n
if data(i,m)==1
data1(l,:)=data(i,1:m-1);
l=l+1;
else
data2(t,:)=data(i,1:m-1);
t=t+1;
end
end
if t>l
data1(l:t-1,1:m-1)=0;
else
data2(t:l-1,1:m-1)=0;
end
for i=1: m-1
thisCol1=data1(:,i);
thisCol2=data2(:,i);
a18(i)=fKLDDist(thisCol1,thisCol2);
end
and then in for loop I computed the distance value between the data1 and data2 and for computation I defined a function fKLDDist (I attached the image of the formula)but I found the error.
function [ fkld ] = fKLDDist( p,q )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
fkld=sum(dot(p,log(p/q)));
end
the error is:
Error using dot (line 33)
A and B must be same size.
Error in fKLDDist (line 4)
fkld=sum(dot(p,log(p/q)));
Error in myFSmethod (line 56)
a18(i)=fKLDDist(thisCol1,thisCol2);
because I used dot(p,log(p/q)) in the function,matlab said A and B must be same size but I don't know how to make p and log(p/q) the same size.I'll be very gratefull to have your opinions. Thanks

채택된 답변

Fabio Freschi
Fabio Freschi 2019년 12월 10일
Try
fkld=sum(dot(p,log(p./q)));
with the element-wise division ./
  댓글 수: 1
phdcomputer Eng
phdcomputer Eng 2019년 12월 11일
@Fabio Freschi Thanks greatly for your attention. the problem of the function in my question is solved by your advice, but for one function It has error:
function [ fkdg ] = fKDivergence( p,q )
fkdg=sum(p.*(log((2.*p)/(p+q))));
end
I used element-wise for several functions and their errors solved.
I used element-wise operator .* in the codes but MATLAB shows this error
Error using .*
Matrix dimensions must agree.
Error in fKDivergence (line 5)
fkdg=sum(p.*(log((2.*p)/(p+q))));
I'll be very gratefull to have your opinion. Thankskd.jpg

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Statistics and Machine Learning Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by