Implementing an equation in Matlab

조회 수: 1 (최근 30일)
Saugata Bose
Saugata Bose 2019년 6월 27일
댓글: darova 2019년 6월 28일
Hi,
I am trying to implement an equation in a form like, if a*b<1 then, x=-b/N otherwise x=0, where N= size of a. The equation is simple, but the data I recieve for a and b are differnt formet. a is a 4-d numeric array and b is a categorical array. Would you please suggest me how could I implement this equation?
Sample Input
a= val(:,:,1,1)=0.55
val(:,:,2,1)=0.45
val(:,:,1,2)=0.67
val(:,:,2,2)=0.33
b=
0
1
I have tried to solve it my way like following:
[m,n,o,p]=size(a,4);
x=zeros(m,n,o,p);
c=double(string(categorical(b)));
for i=1:p
for j=1:o
mul=c(i)*a(:,:,j,i);
if(mul<1)
x(:,:,j,i)=-c(i)/p;
else
x(:,:,j,i)=0;
end
end
end
Is it the right way to solve the equation?
  댓글 수: 5
Saugata Bose
Saugata Bose 2019년 6월 28일
@darova: As the equation is like if the result of a*b is less than 1 then the value of X would be -b/N.
Inb my problem a must be a 4-d numeric array of shape (1 x 1 x i x j), where i is the category, and j is the observation ID and b must be taken as a categorical vector.
a= val(:,:,1,1)=0.67 (it means, the probability of the first observation stays in category 1 is 0.67)
val(:,:,2,1)=0.33( it means, the probability of the first observation stays in category 2 is 0.33)
However, c(1) refers the calue of first category, in my case, it is 0 where as c(2) refers the value of second category which is 1 here.
darova
darova 2019년 6월 28일
If mul is a variable (1x1) your above code is OK.
mul=c(i)*a(:,:,j,i);
Also you don't have to use else statement because x matrix is already zeros

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 Sequence and Numeric Feature Data Workflows에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by