MATLAB: Using the Probability to change values of variables

fsolvevariables

Hello I have 10 known variables (x1=5,x2=6,…..,x10=11). I want to force x1 to be zero at the first step. Then force x2 to be zero at the second step while x1=5. then force x3 to be zero at the third step while x1=5,x2=6. After the 10th step I will start to force two variables to be zero. for example,at 11th step x1=x2=0, and at 12th step x1=x3=0 while x2=6…etc. I will use the process until all of them will be zero. please, how to do that by matlab? thanks

Best Answer

Like all tasks in MATLAB, this is much easier once you learn to think in terms of vectors and arrays rather than in terms of individual separate variables:
X = 1:5; % define the values here
%
% Create binary matrix:
N = numel(X);
M = [1;0];
for k = 1:N-1
S = numel(M(:,1));
M = [M,ones(S,1);M,zeros(S,1)];
end
% Sort binary matrix into the required order:
[~,idx] = sort(sum(M,2),'descend');
% Multiply binary with X to get output rows:
out = bsxfun(@times,X,M(idx,:))
and creates this matrix, where each column corresponds to one of your variables:
out =
1 2 3 4 5
0 2 3 4 5
1 0 3 4 5
1 2 0 4 5
1 2 3 0 5
1 2 3 4 0
0 0 3 4 5
0 2 0 4 5
1 0 0 4 5
0 2 3 0 5
1 0 3 0 5
1 2 0 0 5
0 2 3 4 0
1 0 3 4 0
1 2 0 4 0
1 2 3 0 0
0 0 0 4 5
0 0 3 0 5
0 2 0 0 5
1 0 0 0 5
0 0 3 4 0
0 2 0 4 0
1 0 0 4 0
0 2 3 0 0
1 0 3 0 0
1 2 0 0 0
0 0 0 0 5
0 0 0 4 0
0 0 3 0 0
0 2 0 0 0
1 0 0 0 0
0 0 0 0 0
For this example I defined the vector X to have five values: you simply need to define X to have eleven values.