creating hyperbolic tangent function for matrix based on another matrix

5 Ansichten (letzte 30 Tage)
I'm having issues trying to create a hyperbolic tangent function for a probability distribution. Right now, I have a matrix based on two vectors subtracting, and then based on the sign of an element in that matrix, there's a probability of either 0.9, 0.5, 0.1
So this is what I originally had:
cap=15;
z=linspace(1,cap,cap)';
y=linspace(1,cap,cap)';
f=@(x,y) x-y;
M=f(z,y.');
p=zeros(size(M));
z1=z+1;
z1(z1>cap)=cap;
for j=1:15
for k=1:15
if M(z1(j),k)>0
p(z1(j),k)=0.9;
elseif M(z1(j),k)<0
p(z1(j),k)=0.1;
else
p(z1(j),k)=0.5;
end
end
end
Now what I'm trying to do it make p more of a curve instead of a step. I know you use the tanh, but I'm not sure how to make it with the p matrix being based on the matrix M. I also never want it to be 0, 1, or -1, I always want there to be a possibility that the individual goes to patch 2.
To put it into context of what I'm using this for, an individual has an estimation of quality between two patches (z and y) and matrix M represents the difference in estimation between the two patches. p represents the probability of going to patch 1 (z), so when M is positive (i.e. patch 1 estimation is higher than patch 2) the individual has a higher chance of going to patch 1. Right now it's just stepwise, but I'm wanting to make it a curve where, as the difference in estimation gets bigger, the probability as gets bigger/smaller.

Akzeptierte Antwort

Abhishek Kumar Singh
Abhishek Kumar Singh am 28 Mai 2024
Bearbeitet: Abhishek Kumar Singh am 4 Jun. 2024
Hi @Kitt,
I roughly understand that you want to create a mathematical model that uses hyperbolic tangent function to smoothly vary the probability of choosing between two options based upon the difference in their estimated qualities.
Since tanh outputs values in the range (-1,1), you can scale and shift its output to lie within the desired probability range, such as (0.1, 0.9). Then you can implement a scaling factor to control the steepness of the curve. Refer to the snippet below:
cap = 15;
z = linspace(1, cap, cap)';
y = linspace(1, cap, cap)';
f = @(x, y) x - y;
M = f(z, y.');
% Scaling factor for tanh to adjust steepness of the transition
k = 0.2; % Example value, adjust this as needed
% Scale and shift tanh output to fit within the desired range (0.1 to 0.9)
% The formula below transforms the output range of tanh to (0.1, 0.9)
p = (0.9 - 0.1) / 2 * tanh(k * M) + (0.9 + 0.1) / 2;
The direct assignment and loop are no longer needed because the entire matrix 'p' is calculated in one step.
I used MATLAB's imagesc function for a color-coded visualization:
figure; % Creates a new figure
imagesc(p); % Plots the matrix with color coding
colorbar; % Shows the color scale
title('Probability Distribution with tanh Function');
xlabel('Patch 2 Quality Estimation');
ylabel('Patch 1 Quality Estimation');
axis square; % Makes the plot square in aspect ratio
Hope it helps!
  1 Kommentar
Kitt
Kitt am 28 Mai 2024
Thanks so much for the help, that makes it look a lot smoother and really helps to get rid of all the 1s!!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by