Matlab if/for-Statement Assign Value - matlab

I have an if statement something like
t(2,5)
P(6,1)
if x:length(t)
t(2,3)>P
P=0
elseif t(2,3)<P
P=1
end
basically what I want to achieve is a loop that runs through t and compares it to p, if the value of t is smaller than the value of P a new matrix should be created and record the value as "1", if the value of t is bigger than the value of P record value as "0", I can't get it to work unfortunately.

If you just want to check if the values in array/matrix t is less than a certain constant value, say P, then better run t<P. That will do the job.
If t and P are both matrices, of equal length, then also you can use t<P. No need for loops or if statements.

Related

length of a just one array dimension in minizinc

If I define my array like this:
array[_, 1..2] of J: A;
where J is a enum, what function can I use to get the length of the first dimension of A? The length function returns 14 (and states that it doesn't work for multiple dimensions). I had it hard-coded like so in my testing, but it won't always be 13:
constraint forall(row in 1..13)(S[A[row, 2]] >= C[A[row, 1]]);
The length function is really only meant for getting the number of elements. When you want the retrieve indexes of an array, you should instead use the index_set function. This returns the actual index set, which you can iterate over: i in index_set(A)
For multidimensional arrays there are specific functions for each index set of the array. For example, to get the first index set from a two-dimensional array (as in your code fragment) you can use the function index_set_1of2

Print the name of a variable on upon a plot/figure

Is it possible to refer back to/access the names of variables (say nx1 arrays) that make up a matrix? I wish to access them to insert there names into a plot or figure (as a text) that I have created. Here is an example:
A = [supdamp, clgvlv,redamp,extfanstat,htgvlv,occupied,supfanspd]
%lots of code here but not changing A, just using A(:,:)'s
%drawn figure
text(1,1,'supdamp')
...
text(1,n,'supfanspd')
I have failed in an attempt create a string named a with their names in so that I could loop through a(i,1), then use something like text(1,n,'a(i,1)')
Depending on your problem, it might make sense to use structures with dynamical field names.
Especially if your data in the array have some meaning other than just entries of a matrix in linear algebra sense.
# name your variables so that your grandma could understand what they store
A.('supdamp') = supdamp
A.('clgvlv') = clgvlv
...
fieldsOfA = fieldnames(a)
for n = 1 : numel(fieldsOfA )
text(1, n, fieldsOfA{n})
end

Matlab set param from array

In Matlab, in order to change the value of a block I do
set_param('model/V','Amplitude','100')
and the value of V is 100. But if I do
for i=1:10
set_param('model/V','Amplitude','P(i)')
...
end
The it stores the value of V as P(i). But in order to access the i-th element of the 20-by-1 P matrix, I need to refer to it by P(i). What is my error?
Change the value to string using:-
set_param('model/V','Amplitude',num2str(P(i)) );
Also it will set the value of 'model/V' to P(20) i.e. last one.
You might want to loop through the current blocks too
Something like : (just example)
set_param(['model/V' num2str(i)],'Amplitude',num2str(P(i)) );
for model/V1, model/V2,...model/V20 .

Nested if statement in for loop

So heres my script
function printPower
sum=0;
filename=input('Enter a filename: ','s');
power=load(filename);
for i=1:length(power);
if power(i)>=0;
sum=sum+power(i);
end
TP=sum/24;
end
fprintf('Total power: %.1f kWh.\n', TP);
There are negative values in the text file im loading and I want it to only sum the positive ones but it still sums all values.
You could just replace your loop with something like
total = sum(power(power>=0))/24
Personally I think that using the name of a Matlab intrinsic function, such as sum, as a variable name is just asking for trouble though I'm not sure it's caused a problem in your case. That's why the lhs of my statement is the variable total.

going from command window (as script) to function

the program is working perfectly but when i am changing it to function the follwing error is beeing displayed:
[Parent1index, Parent1Position, alldcel] = Parent1n(TotalnoOfGrids, noOfNodes, Penalties, test)
??? Index exceeds matrix dimensions.
Error in ==> Parent1n at 10
[~,index]=min(alldcel{t});
Because alldcell{t} may not exist for some values of t if the condition to assign values to it in
if Penalties{t}(r)== 0;
alldcel{t}(r)=inf;
end
is never satisfied. Assume for some t that all values of Penalties{t} are different than zero. Then you would never assign inf to alldcell{t}. That means, you are only extending the cell array alldcell when Penalties{t} is zero for some r. If the condition is never satisfied, alldcell{t} will not exist and asking for it will give you a cell array error.
You should at least initialize it using alldcell = cell(TotalnoOfGrids,1).
Also, comparing for equality to zero using a==0 is not a good idea. You should use abs(a)<tol for some small value tol.
ok with this code the functiion worked:if Penalties{t}(r)> 0;
alldcel {t}(r)=alldcel{t}(r);
else alldcel {t}(r)=inf; but interchanging if statement with else did not