Please could someone explain the basics of how MATLAB updates codes in a script?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I'm kind of teaching myself how to use MATLAB and I try to write my codes like I would do on paper but it does not work out as I expect. For example, a code below:
spikeMat is a vector that has the values of either 0 or 1. I generated this using a function that I created
I am trying to say that anytime spikeMat(t) = 1, then fac_P(t) at timepoint t should increase by this formula: fac_P(t)= fac_P(t)+ (0.1 * ( 1 - fac_P(t))).
Then fac_P(t+1) should be :
fac_P(t+1)= facResting_P0 + ((fac_P(t)- facResting_P0) * exp(-dTime/fac_timeConst));
After this, the value of fac_P should not change till the next time spikeMat(t) = 1
dTime = 10^-3;
time = 0: dTime: 1;
fac_P = zeros(1, length(time));
fac_P(1) = 0;
facResting_P0 = 0;
fac_timeConst= 0.1;
for t = 1: length(time)-1
[spikeMat] = poissonSpikeGen(10^-3, 100, 1, 1); %a function that I created elsewhere
t_spike = find( spikeMat== 1);
if ismember(t, t_spike)
fac_P(t)= fac_P(t)+ (0.1 * ( 1 - fac_P(t)));
fac_P(t+1)= facResting_P0 + ((fac_P(t)- facResting_P0) * exp(-dTime/fac_timeConst));
end
fac_P(t + 1) = fac_P(t);
end
0 commentaires
Réponse acceptée
Bob Thompson
le 1 Mar 2019
Did you delete and repost this question? I swear I saw it earlier today.
A couple of things about your code.
1) It doesn't look like you need to calculate spikeMat inside the for loop, because none of the inputs for your function involve t so the results will be the same every time you run the function. Keeping it inside the loop is just a waste of time.
2) Does spikeMat have an element that corresponds to every t? If so, you can replace some stuff.
% Original
t_spike = find(spikeMat == 1);
if ismember(t, t_spike)
% Replacement
if spikeMat(t) == 1
3) Inside your if condition you define a new value for fac_P(t) and fac_P(t+1), but outside your if statement you define fac_P(t+1). This means that even if you do change fac_P(t+1) inside the if, it will always be overwritten by the command which follows. I would suggest putting the second fac_P(t+1) command in an 'else' condition.
if spikeMat(t) == 1
fac_P(t)= fac_P(t)+ (0.1 * ( 1 - fac_P(t)));
fac_P(t+1)= facResting_P0 + ((fac_P(t)- facResting_P0) * exp(-dTime/fac_timeConst));
else
fac_P(t + 1) = fac_P(t);
end
4) Do you know how to put debug markers in your code? I find that they are generally the best tool to make sure your if statements are triggering correctly.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!