Let say I have
A = [0 0 0 0 1 1 1 1 1 0 0 1 1 1 1 0 0 0 1 1 1 1 1 ]
B = [0.01 0.02 0.01 0.03 0.04 0.05 0.07 0.04 0.03 0.01 0.01 0.03 0.04 0.03 0.02 0.01 0.011 0.02 0.03 0.04 0.05 0.04 0.01]
How can I rescale A follow maximum number of B.
The result should be
C = [0 0 0 0 0.07 0.07 0.07 0.07 0.07 0 0 0.04 0.04 0.04 0.04 0 0 0 0.05 0.05 0.05 0.05 0.05]
You can use accumarray like so:
subs = cumsum([diff(A) > 0, 0]).*A + 1; %//Similar to bwlabel if you have the image processing toolbox...
maximums = accumarray(subs(:), B(:), [], #max);
maximums(1) = 0;
C = maximums(subs)
Is it what you want to do?
C = A*max(B)/max(A)
Related
I'm trying to generate a matrix such that:
Diagonal elements are 1
All other elements are 0.5
I'm trying to modify the example for the identity matrix:
{x=/:x}#til 4
to squeeze in my special function:
shrinkfn: {$[x=y;1;0.5]}
but I'm struggling. What's the best way to do this?
q)m:{x=/:x}#til 4
q)?'[m;1;0.5]
1 0.5 0.5 0.5
0.5 1 0.5 0.5
0.5 0.5 1 0.5
0.5 0.5 0.5 1
Alternative method:
https://code.kx.com/phrases/matrix/#identity-matrix-of-order-x
q)f:{(2#x)#1f,x#.5}
q)f 5
1 0.5 0.5 0.5 0.5
0.5 1 0.5 0.5 0.5
0.5 0.5 1 0.5 0.5
0.5 0.5 0.5 1 0.5
0.5 0.5 0.5 0.5 1
Explanation:
we can use the the following notation to create a matrix:
q)3 3#til 9
0 1 2
3 4 5
6 7 8
when the list runs out of elements it repeats:
q)3 2#til 4
0 1
2 3
0 1
with 5 by 5 matrix the the next diagonal is always 6 places, thus the list is of length 6:
q)5 5#1 .5 .5 .5 .5 .5
1 0.5 0.5 0.5 0.5
0.5 1 0.5 0.5 0.5
0.5 0.5 1 0.5 0.5
0.5 0.5 0.5 1 0.5
0.5 0.5 0.5 0.5 1
For the sake of variety another option is:
{0.5 1f x=/:x}til 4
This will use the boolean lists (0 or 1b) to index into our two element array and distribute the values accordingly along the matrix.
q){x=/:x}til 4
1000b
0100b
0010b
0001b
q){0.5 1f x=/:x}til 4
1 0.5 0.5 0.5
0.5 1 0.5 0.5
0.5 0.5 1 0.5
0.5 0.5 0.5 1
I am solving the following linear programming problem, I am guiding myself on the documentation
Documentation Matlab
Every time I execute I get the following error
Dimensions of matrices being concatenated are not consistent.
I have the objective function and restrictions as follows
%F.O
f=[0.669 0.654 0.503 0.683 0.670 0.673 0.749 0.655 0.660 0.583 1.243 0.639 2.024 2.156 1.672 0.473 0.139 0.687];
%Restrictions
Aeq=[0.1 0.12 0.335 0.15 0.18 0.19 0.12 0.15 0.15 0.15 0.15 0.11 0.13 0.46;
0.3 0.24 0.03 0.05 0.04 0.27 0.03 0.24 0.15 0.52 0.52;
0.1 0.12 0.31 0.15 0.19 0.08 0.2 0.12 0.15 0.50 0.34 0.44;
0.26 0.50;
0.06 0.17];
b=[285.71; 305.33; 450; 262.50; 41.50];
and I execute it with the following command
x=linprog(f,Aeq,b)
The restrictions take them out of the following exercise
Exercise
This should be the result of z
Result
A matrix must have the same number of columns in each row. Aeq in your case has different number of columns.
How to fix it?
The variables missing in the equations of constraints have zero coefficients. So:
%Objective Function
%X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14 X15 X16 X17 X18
f = [0.669 0.654 0.503 0.683 0.670 0.673 0.749 0.655 0.660 0.583 1.243 0.639 2.024 2.156 1.672 0.473 0.139 0.687];
A = []; b = []; %No inequality constraints
%Equality Constraints are:
%X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14 X15 X16 X17 X18
Aeq=[0.1 0.12 0.335 0.15 0.18 0.19 0.12 0.15 0.15 0.15 0 0.15 0.11 0 0.13 0 0 0.46; %Nitrogeno
0.3 0.24 0 0.03 0.05 0.04 0.27 0.03 0.24 0.15 0 0 0.52 0.52 0 0 0 0 ; %Fosforo
0.1 0.12 0 0.31 0.15 0.19 0.08 0.2 0.12 0.15 0.50 0 0 0.34 0.44 0 0 0 ; %Potasio
0 0 0 0 0 0 0 0 0 0 0 0.26 0 0 0 0 0.50 0 ; %Calcio
0 0 0 0 0.06 0 0 0 0 0 0 0 0 0 0 0.17 0 0]; %Magnesio
beq = [285.71; 305.33; 450; 262.50; 41.50]; %What you defined as 'b' is actually `beq`
lb = zeros(18,1); ub = inf(18,1); %Bounds
[x, Optimal_sol] = linprog(f, A, b, Aeq, beq, lb, ub);
>> x
x =
1.0e+03 *
0.7142
0
0
1.0268
0
0
0
0
0
0.4018
0
0
0
0
0
0.2441
0.5250
0
>> Optimal_sol
Optimal_sol =
1.6018e+03
I have a big matrix lets say 100000x13.
I need to sum specific columns of this matrix.
For example:
matrix = [0.70 0.30 0 0 0.15 0.21 0.58 0.06 0.00 1.00 0 0 1.00;
0.70 0.00 0 0 0.00 0.00 0.07 0.06 0.00 0.80 0 0 1.00;
0.70 0.00 0 0 0.00 0.00 0.58 0.06 0.00 1.00 0 0 0.94];
inputVect = [4 4 3 2];
idx2 = cumsum(inputVect);
idx1 = [1 idx2(1:end-1)+1];
result = (1-sum(matrix(:,idx1(1):idx2(1)),2)) + (1-sum(matrix(:,idx1(2):idx2(2)),2))+(1-sum(matrix(:,idx1(3):idx2(3)),2)) + (1-sum(matrix(:,idx1(4):idx2(4)),2));
The thing is inputVect needs to be a function argument that I don't allways know the size. And because of the size of the matrix I should also avoid for loops. Any help would be much appreciated.
I have a vector:
0.02
-0.02
0
-0.02
-0.08
-0.05
-0.04
-0.1
0
0.05
0.05
0.05
0.08
0.04
How do I normalize this with the first value starting at 100?
Simply divide by the first element and multiply by 100:
a = [0.02 -0.02 0 -0.02 -0.08 -0.05 -0.04 -0.1 0 0.05 0.05 0.05 0.08 0.04]
b = a ./ a(1) * 100
b =
100 -100 0 -100 -400 -250 -200 -500 0 250 250 250 400 200
myArr = [0.02 -0.02 0 -0.02 -0.08 -0.05 -0.04 ...
-0.1 0 0.05 0.05 0.05 0.08 0.04]
myArr = 100*myArr/myArr(1)
I am trying to make a program in matlab to get this numbers:
0 1 0
0 0.8 0.2
0 0.6 0.4
0 0.4 0.6
0 0.2 0.8
0 0 1
0.1 0.9 0
0.1 0.7 0.2
0.1 0.5 0.4
0.1 0.3 0.6
0.1 0.1 0.8
0.1 0 0.9
and so on but I cant make the program to reduce the values of the second and third column when the first column increases. This is my code. Thanks
lai=0:0.1:1;
laj=1:-0.2:0;
lat=0:0.2:1;
for i=1,length(lai)
for j=1,i
for t=1,j
j
lam1(1,:)=lai;
lam2(1,:)=laj;
lam3(1,:)=lat;
end
end
end
Try this and do some thinking for what you require.
for i=0:0.1:0.1
for j=0:0.2:1
disp([i,j,1-j])
end
end