I have a bug in a MATLAB code and I cannot find it - matlab

I am trying to make a simple linear programming optimization, using MATLAB R2012b. I came up with this code:
clear;
y=1663;
f=[100; 100; 100; 100; 100; 100; 100; 100];
A=[1/867.2 1/704.7 1/619.2 1/679 1/770.3 1/658.5 1/749 1/783.1
-105.2 -74.4 -94.3 -88.2 -108.9 999 -112.7 -91.4
-94.1 -71.2 -87.6 -87.8 -99.4 999 -100 -81.2
0.5 0.6 4.5 3 28 15 4.5 12.5
0.78 2.11 0.01 0.01 0.56 0.09 0.02 0.9
93.63 16.3 0.01 0.01 1.44 0.09 0.03 37.64
0.82 3.23 0.03 0.36 3.13 29.97 0.04 16.16];
b=[y/0.775; -95*y; -85*y; 10*y; 1*y; 35*y; 18*y];
Aeq=[1; 1; 1; 1; 1; 1; 1; 1];
beq=y;
lb=zeros(8,1);
hb=[550; 250; 50; 200; 50; 350; 400];
linprog(f, A, b, Aeq, beq, lb, hb);
When I try to run this source, it says that The number of rows in A must be the same as the number of elements of b.
A has 7 rows and b has 7 elements, as it can be seen. I cannot see the mistake. Where is it?
Thanks.

You have to redefine Aeq. Since you have only one equality constraint, it should be 1xn. I also included x as an output of linprog.
Aeq=[1, 1, 1, 1, 1, 1, 1, 1];
x=linprog(f, A, b, Aeq, beq, lb, hb);
The result is
x =
200.7466
59.3486
30.1325
62.8837
35.7733
1.6669
341.6570
930.7914

Related

Writing a MATLAB code using an algorithm for a reciprocal matrix

I want to write a code for the following algorithm using MATLAB.
**Input:** An square reciprocal matrix T = (tij) for (i, j = 1, 2, . . . , n),
**Output:** A square reciprocal matrix C = (cij) for (i, j = 1, 2, . . . , n);
1: for i = 1; i < n; i ++ do
2: for j = i ; j < n; j ++ do
3: cij = sqrt(tij /tji );
4: cji = 1/cij ;
5: end for
6: end for
I have the following matrix T:
T=[...
0.08 0.02 0.34 0.67;...
0.01 0.08 0.17 0.34;...
0.02 0.04 0.09 0.18;...
0.01 0.02 0.04 0.09]
The answer C I found on a paper is:
C = [...
1 2 4 8;...
0.50 1 2 4;...
0.25 0.50 1 2;...
0.13 0.25 0.50 1]
So far, I have tried the following code, but I am not certain about it. I couldn't find the exact answer C above. Any idea or help, please?
C=zeros(n,n);
for i = 1:n
for j = i:n
C(i,j) = sqrt(T(i,j)/T(j,i));
C(j,i) = 1/C(i,j) ;
end
end
C;

vectorise foor loop with a variable that is incremented in each iteration

I am trying to optimise the running time of my code by getting rid of some for loops. However, I have a variable that is incremented in each iteration in which sometimes the index is repeated. I provide here a minimal example:
a = [1 4 2 2 1 3 4 2 3 1]
b = [0.5 0.2 0.3 0.4 0.1 0.05 0.7 0.3 0.55 0.8]
c = [3 5 7 9]
for i = 1:10
c(a(i)) = c(a(i)) + b(i)
end
Ideally, I would like to compute it by writting:
c(a) = c(a) + b
but obviously it would not give me the same results since I have to recalculate the value for the same index several times so this way to vectorise it would not work.
Also, I am working in Matlab or Octave in case that this is important.
Thank you very much for any help, I am not sure that it is possible to be vectorise.
Edit: thank you very much for your answers so far. I have discovered accumarray, which I did not know before and also understood why changing the for loop between Matlab and Octave was giving me such different times. I also understood my problem better. I gave a too simple example which I thought I could extend, however, what if b was a matrix?
(Let's forget about c at the moment):
a = [1 4 2 2 1 3 4 2 3 1]
b =[0.69 -0.41 -0.13 -0.13 -0.42 -0.14 -0.23 -0.17 0.22 -0.24;
0.34 -0.39 -0.36 0.68 -0.66 -0.19 -0.58 0.78 -0.23 0.25;
-0.68 -0.54 0.76 -0.58 0.24 -0.23 -0.44 0.09 0.69 -0.41;
0.11 -0.14 0.32 0.65 0.26 0.82 0.32 0.29 -0.21 -0.13;
-0.94 -0.15 -0.41 -0.56 0.15 0.09 0.38 0.58 0.72 0.45;
0.22 -0.59 -0.11 -0.17 0.52 0.13 -0.51 0.28 0.15 0.19;
0.18 -0.15 0.38 -0.29 -0.87 0.14 -0.13 0.23 -0.92 -0.21;
0.79 -0.35 0.45 -0.28 -0.13 0.95 -0.45 0.35 -0.25 -0.61;
-0.42 0.76 0.15 0.99 -0.84 -0.03 0.27 0.09 0.57 0.64;
0.59 0.82 -0.39 0.13 -0.15 -0.71 -0.84 -0.43 0.93 -0.74]
I understood now that what I would be doing is rowSum per group, and given that I am using Octave I cannot use "splitapply". I tried to generalise your answers, but accumarray would not work for matrices and also I could not generalise #rahnema1 solution. The desired output would be:
[0.34 0.26 -0.93 -0.56 -0.42 -0.76 -0.69 -0.02 1.87 -0.53;
0.22 -1.03 1.53 -0.21 0.37 1.54 -0.57 0.73 0.23 -1.15;
-0.20 0.17 0.04 0.82 -0.32 0.10 -0.24 0.37 0.72 0.83;
0.52 -0.54 0.02 0.39 -1.53 -0.05 -0.71 1.01 -1.15 0.04]
that is "equivalent" to
[sum(b([1 5 10],:))
sum(b([3 4 8],:))
sum(b([6 9],:))
sum(b([2 7],:))]
Thank you very much, If you think I should include this in another question instead of adding the edit I will do so.
Original question
It can be done with accumarray:
a = [1 4 2 2 1 3 4 2 3 1];
b = [0.5 0.2 0.3 0.4 0.1 0.05 0.7 0.3 0.55 0.8];
c = [3 5 7 9];
c(:) = c(:) + accumarray(a(:), b(:));
This sums the values from b in groups defined by a, and adds that to the original c.
Edited question
If b is a matrix, you can use
full(sparse(repmat(a, 1, size(b,1)), repelem(1:size(b,2), size(b,1)), b))
or
accumarray([repmat(a, 1, size(b,1)).' repelem(1:size(b,2), size(b,1)).'], b(:))
Matrix multiplication and implicit expansion and can be used (Octave):
nc = numel(c);
c += b * (1:nc == a.');
For input of large size it may be more memory efficient to use sparse matrix:
nc = numel(c);
nb = numel(b);
c += b * sparse(1:nb, a, 1, nb, nc);
Edit: When b is a matrix you can extend this solution as:
nc = numel(c);
na = numel(a);
out = sparse(a, 1:na, 1, nc, na) * b;

How to interpolate matrix to get specific values

I have this matrix in MATLAB:
x = [NaN -2 -1 0 1 2;
1 0.21 0.15 0.34 0.11 0.32;
2 0.14 0.10 0.16 0.31 0.11];
The first row represents the location of the values following X coordinates.
I shift the first row by -0.63, so x becomes:
New_x = [NaN -2.63 -1.63 -0.63 0.37 1.37;
1 0.21 0.15 0.34 0.11 0.32;
2 0.14 0.10 0.16 0.31 0.11];
How can I use interpolation to get the values at specific coordinates of the New_x matrix that we have in the x matrix? ([-2 -1 0 1 2] points)
New_xInterp = [NaN -2.63 .. -2 .. -1.63 .. -1 .. -0.63 .. 0 .. 0.37 .. 1 .. 1.37 .. 2;
1 0.21 .. ? .. 0.15 .. ? .. 0.34 .. ? .. 0.11 .. ? .. 0.32 .. ?;
2 0.14 .. ? .. 0.10 .. ? .. 0.16 .. ? .. 0.31 .. ? .. 0.11 .. ?];
I want to get the '?' values. I tried to use interp2 function but I don't know which step or 2^k-1 interpolated points between coordinates values I have to have in order to get the points like -2, -1, 0, 1, 2.
Thanks !
Since you do not have 2D data, you are only interpolating on one dimension, you only need the function interp1.
This function can work on vector or matrices if necessary, but it require a slight reorganisation of your data.
%% Input
M = [NaN -2 -1 0 1 2;
1 0.21 0.15 0.34 0.11 0.32;
2 0.14 0.10 0.16 0.31 0.11];
%% Demultiplex inputs
x = M(1,2:end).' ; % extract X values, reorder in column
y = M(2:end,2:end).' ; % extract Y values, reorder in columns
%% Interpolate
xn = sort( [x-0.63 ; x] ) ; % Generate the new_x target values
yn = interp1( x-0.63 , y , xn ,'linear','extrap') ; % Interpolate the full matrix in one go
At this point you have your new xn and yn values in columns:
xn= yn=
-2.63 0.21 0.14
-2 0.1722 0.1148
-1.63 0.15 0.1
-1 0.2697 0.1378
-0.63 0.34 0.16
0 0.1951 0.2545
0.37 0.11 0.31
1 0.2423 0.184
1.37 0.32 0.11
2 0.4523 -0.016
I would keep them like that if you have more operations to do on them later on. However, if you want it back into the format you had at the beginning, we can simply rebuild the new full matrix:
%% Rebuild global matrix
Mout = [ M(:,1) , [xn.' ; yn.'] ]
Mout =
NaN -2.63 -2 -1.63 -1 -0.63 0 0.37 1 1.37 2
1 0.21 0.1722 0.15 0.2697 0.34 0.1951 0.11 0.2423 0.32 0.4523
2 0.14 0.1148 0.1 0.1378 0.16 0.2545 0.31 0.184 0.11 -0.016
Maybe you can try interp1 + arrayfun like below
r = sort([x(1,2:end),New_x(1,2:end)]);
New_xInterp = [New_x(:,1),cell2mat(arrayfun(#(k) interp1(New_x(1,2:end),New_x(k,2:end),r),1:size(New_x,1),'UniformOutput',false).')];
which gives
New_xInterp =
NaN -2.63000 -2.00000 -1.63000 -1.00000 -0.63000 0.00000 0.37000 1.00000 1.37000 NA
1.00000 0.21000 0.17220 0.15000 0.26970 0.34000 0.19510 0.11000 0.24230 0.32000 NA
2.00000 0.14000 0.11480 0.10000 0.13780 0.16000 0.25450 0.31000 0.18400 0.11000 NA
The code above used linear interpolation. If you want other options, you can type help interp1 to see more.

how can transform a matrix matlab into file .txt?

I have a matrix proba (size :10 * 5).
proba=[0.5 0.3 0.8 0.9 0.8;
0.50 0.36 0.58 0.58 0.98;
0.1 0.25 0.6 0.8 0.9;
0.5 0.3 0.8 0.9 0.8;
0.2 0.9 0.58 0.58 0.69;
0.58 0.14 0.1 0.2 0.3;
0.25 0.9 0.8 0.7 0.5;
0.58 0.69 0.25 0.1 0.1;
0.1 0.25 0.36 0.2 0.3;
0.5 0.3 0.8 0.9 0.8 ];
I want to transform this matrix into a text file (proba.txt) with which the index column is written and the value of the column for each line as follows :
1 0.5 2 0.3 3 0.8 4 0.9 5 0.8
1 0.50 2 0.36 3 0.58 4 0.58 5 0.98
.
.
.
1 0.5 2 0.3 3 0.8 4 0.9 5 0.8
Please I need help, how can I do it?thanks in advance
You can use this function, it is useful for every matrix.
function data = addIndex(X)
[r, c] = size(X);
index = ones(r, 1);
data = zeros(r, 2 * c);
for i = 1:c
data(:, 2 * i - 1) = i .* index;
data(:, 2 * i) = X(:, i);
end
dlmwrite('proba.txt', data, '\t')
end
you can easily do this using dlmwrite, but first you want to add the column of indexes in front of your matrix:
function result = writematrix(proba)
rowind = 1:size(proba,2);
for t = 1:size(proba,1);
C(t,:,:) = [rowind',proba(t,:)']';
D(t,:) = C(t(:),:);
end
dlmwrite('filename.txt',D,'\t') %//I assume you want tab delimiter, if you want space, it is ' ' instead
%//dlmwrite('filename.txt',D,' ')
end
Note that this will write the text file into your local directory, and that it only works for numerical values, not strings, for strings, it is better to use csvwrite.
EDIT : Ops, didn't read the question fully, this should now work fine.

scale part of an axis in matlab

I have the following image which I want to have the depth axis range like below :
(10 9.5 9 8.5 8 7.5 7 6 5 3 2 1 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0) to show the data between depth 1 and 0 in larger scale, and I have the following code
depths = [10 5 1 0.5 0; 10 5 1 0.5 0] % these are the real depths in meter
contourf(points,depths,RFU15102013_BloomAsMainPoint);
set(gca, 'XTick', points(1) : points(2), 'XTickLabel',{ 'LSB1', 'LSB2'});
ylabel('Depth(m)');
xlabel('Points');
title('Date: 15.10.2013');
this is the image :
how can I do that?
EDIT1
Real Data:
RFU15102013_BloomAsMainPoint = [ 2.71 1.23 1.30 1.20 14.37 ; 2.51 1.36 1.01 1.24 1.15];
points = [1 1 1 1 1; 2 2 2 2 2 ];
depths = [10 5 1 0.5 0; 10 5 1 0.5 0];
As most of a data changes around zero it could be enough to change scaling of Y axis. Here is an example
close all; clear all;
z = [ 2.71 1.23 1.30 1.20 14.37 ; 2.51 1.36 1.01 1.24 1.15];
x = repmat([1; 2], 1, 5);
y = repmat([10 5 1 0.5 0], 2, 1);
% plotting with equally spaced y-s
h = subplot(1,2,1);
contourf(x,y,z);
y2 = log(y + 0.25);
yTicks = linspace(min(y2(1,:)), max(y2(1,:)), 10);
% plotting with logarithmically spaced y-s
h = subplot(1,2,2)
contourf(x,y2,z);
set(h,'YTick', yTicks)
set(h,'YTickLabel', exp(yTicks) - 0.25);
print('-dpng','scaling.png')
The result
This way any monotonic continuous function for axis scaling can be applied.
You could use UIMAGE - UIMAGESC from the mathworks file exchange and set the y values to emphaisize points in 1 to 0 range.