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);
a24(i)=fkumarjohnson(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 fkumarjohnson (I attached the image of the formula)but I found the error.
function [ fkj ] = fkumarjohnson(p,q )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
fkj=sum(((p*p-q*q).^2)/(2*(dot(p,q)).^(3/2)));
end
the error is:
Error using *
Inner matrix dimensions must agree.
Error in fkumarjohnson (line 4)
fkj=sum(((p*p-q*q).^2)/(2*(dot(p,q)).^(3/2)));
Error in myFSmethod (line 62)
a24(i)=fkumarjohnson(thisCol1,thisCol2);
The matlab said the inner matrix dimensions of two sides of * must agree but I don't know how to make their dimensions equal.I'll be very gratefull to have your opinions. Thanks

댓글 수: 2

Stephen23
Stephen23 2019년 12월 10일
편집: Stephen23 2019년 12월 11일
Are you sure that you want matrix multiplication?
Or do you actually need element-wise multiplication?
phdcomputer Eng
phdcomputer Eng 2019년 12월 11일
@Stephen Cobeldick Thanks, I want to calculate the result of the formula, and I guess that the formula needs element wise multipication because I'm computing the distance between every column of the matrix (colon) so all the elements in a column(all instances) is involved in the formula calculations. For element wise multiplication these codes are right?

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

 채택된 답변

Fabio Freschi
Fabio Freschi 2019년 12월 10일

1 개 추천

fkj= sum(.5*((p.^2-q.^2).^2)./((p.*q).^1.5));
Note that .^ .* and ./ operators are used for element-wise operation (see also Stephen's comment).
Note also that dot can give apparently unexpected results for complex inputs (see dot help)

댓글 수: 4

phdcomputer Eng
phdcomputer Eng 2019년 12월 11일
@ Fabio Freschi Thanks, I used your advice and change the code of the formula :
fkj= sum(((p.^2-q.^2).^2)./(2.*((p.*q).^1.5)));
so you think than I should remove dot and replace with element wise operators?
Fabio Freschi
Fabio Freschi 2019년 12월 11일
it is necessary to use element wise operators. you can leave the dot operator. But remember that for complex vectors, the dot product involves a complex conjugate and this could be not know to everyone.
phdcomputer Eng
phdcomputer Eng 2019년 12월 11일
Thanks greatly.
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

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

추가 답변 (1개)

Image Analyst
Image Analyst 2019년 12월 11일

0 개 추천

The error is described in the FAQ

댓글 수: 3

phdcomputer Eng
phdcomputer Eng 2019년 12월 11일
@ Image AnalystThanks, but I didn't understand how to use cell array to solve the problem.
Fabio Freschi
Fabio Freschi 2019년 12월 11일
Use ./ instead of /
Image Analyst
Image Analyst 2019년 12월 11일
Cell arrays were the first FAQ entry. The link I gave you should have sent you directly to the error on "Inner dimensions must agree". It does for me. Perhaps you didn't give it enough time to scroll to the right part of the page.
Anyway the error was caused by you trying to do a matrix division instead of an element-by-element division. Using ./ like Fabio said will fix it.

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

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2019년 12월 10일

댓글:

2019년 12월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by