Matrix operations in MatLab - matlab

I have a 10 X 10 matrix, A, created in MatLab. All the values in the matrix are between 0 and 100. Say that I want to:
Replace all elements of A < 10 with zeros
Replace all elemtns of A > 90 with infinity
Extract all values between 30 and 50 to a new vector.
Can I do this without writing a script? I can easliy do this through a script with some simple for-loops, but are there any shortcuts available? Any help will be greatly appreciated!

All of these things are really easy to do using logical indexing:
Each of the operations above can be done quite easily using a one or two commands. However each operation must be done independent of the other two. You can't do all 3 in one line.
1.
smallIdx = A<10;
A(smallIdx) = 0;
% One Line Version
A(A<10) = 0;
2.
bigIdx = A>90;
A(bigIdx)=inf;
% One Line Version
A(A>90) = inf;
3.
middleIdx = A>30 & A<50;
newVector = A(middleIdx);
% One Line Version
newVector = A(A>30 & A<50);
new vector is a vector and wont be square like A was

Set up any 3 × 3 matrix a. Write some command-line statements to perform the following
operations on a:
(a) interchange columns 2 and 3;
(b) add a fourth column (of 0s);
(c) insert a row of 1s as the new second row of a (i.e., move the current second and third rows
down);
(d) remove the second column.

Related

About the matrix indexing

I have a Zeros-matrix P of size (16,16); I need to replace first 4 rows in columns 1:4:end by 1, then next 4 rows in columns 2:4:end by one, and so on.
I have done this, but I noticed that in second rows, the ones are repeated in columns 1 and 2, however what I want is to be only in columns 1:4:end.
Here is the code I made:
P = zeros(16,16);
for i = 1 : 4
P(i:i*4,i:4:end)=1;
end
Could you help to solve that issue. ?
Shift the selected row by (ii-1)*4
P = zeros(16,16);
for ii = 1:4
P([1:4]+(ii-1)*4,ii:4:end)=1;
end
BTW: Avoid to use i as a variable since it's the matlab in-built function that return the imaginary unit.

MATLAB: For loop structure [duplicate]

This question already has answers here:
Element-wise array replication in Matlab
(7 answers)
Closed 6 years ago.
This is a basic program but since I'm new to MATLAB, I'm not able to figure out the solution.
I have a column vector "Time" in which I want to print value "1" in first 147 cells, followed by "2" in 148 to 2*147 cells and so on. For that, I have written the following script:
Trial>> c=1;
Trial>> k=0;
Trial>> for i = c:146+c
Time(i,1)=1+k;
c=i;
k=k+1;
end
I know I need to iterate the loop over "Time(i,1)=1+k;" before it executes the next statement. I tried using break but that's not supposed to work. Can anyone suggest me the solution to get the desired results?(It was quite simple in C with just the use of curly braces.)
I am sure you don't want to run c=i; in every iteration.
My code should work for you:
x = 10; % Replace 10 by the max number you need in your array.
k = 1;
for i = 1 : x * 147
Time(i, 1) = k;
if rem(i, 147) == 0
k = k + 1;
end
end
This is the prime example of a piece of code that should be vectorized can help you understand vectorization. Your code can be written like this:
n = 147;
reps = 10; %% Replace this by the maximum number you want your matrix to have
Time = reshape(bsxfun(#plus, zeros(n,1), 0:reps), 1, []);
Explanation:
Let A be a column vector (1 column, n rows), and B be a row vector (1 row, m columns.
What bsxfun(#plus, A, B) will do here is to add all elements in A with all elements in B, like this:
A(1)+B(1) A(1)+B(2) A(1)+B(3) ... A(1)+B(m)
A(2)+B(1) A(2)+B(2) ............. A(2)+B(m)
............................................
A(n)+B(1) A(n)+B(2) .............. A(n)+B(m)
Now, for the two vectors we have: zeros(n,1), and 0:reps, this will give us;
0+0 0+1 0+2 0+reps
0+0 0+1 0+2 0+reps
% n rows of this
So, what we need to do now is place each column underneath each other, so that you will have the column with zeros first, then the row with ones, ... and finally the one with reps (147 in your case).
This can be achieved by reshaping the matrix:
reshape(bsxfun(#plus, zeros(n,1), 0:reps), [], 1);
^ ^ ^ ^
| | | Number of rows in the new matrix. When [] is used, the appropriate value will be chosen by Matlab
| | Number of rows in the new matrix
| matrix to reshape
reshape command
Another approach is using kron:
kron(ones(reps+1, 1) * 0:(n-1)
For the record, a review of your code:
You should always preallocate memory for matrices that are created inside loops. In this case you know it will become a matrix of dimensions ((reps+1)*n-by-1). This means you should do Time = zeros((reps+1)*n, 1);. This will speed up your code a lot.
You shouldn't use i and j as variable names in Matlab, as they denote the imaginary unit (sqrt(-1)). You can for instance do: for ii = 1:(n*147) instead.
You don't want c=i inside the loop, when the loop is supposed to go from c to c + 146. That doesn't make much sense.
You can use repmat,
x = 10; % Sequence length (or what ever it can be called)
M = repmat(1:x,147,1); % Replicate array 1:x for 147 columns
M = M(:); % Reshape the matrix so that is becomes a column vector.
I can assume that this is a task to practice for loops, but this will work.
An alternative solution may be to do
n = 147;
reps = 10;
a = ceil( (1:(n*reps)) / n);
You first construct an array with the length you want. Then you divide, and round of upwards. 1 to 147 will then become 1.

All possible combinations such that sum of all numbers is a fixed number

I need to find all possible combinations of numbers 1:8 such that sum of all elements is equal to 8
The combinations need to be arranged in an ascending order.
Eg
1 7
2 2 4
1 3 5
1 2 2 3
1 1 1 1 1 1 1 1
A number can repeat itself. But a combination must not..
i.e 1 2 2 3 and 2 1 2 3
I need the the solution in ascending order So there will be only one possibility of every combination
I tried a few codes online suggested on Find vector elements that sum up to specific number in MATLAB
VEC = [1:8];
NUM = 8;
n = length(VEC);
finans = zeros(2^n-1,NUM);
for i = 1:(2^n - 1)
ndx = dec2bin(i,n) == '1';
if sum(VEC(ndx)) == NUM
l = length(VEC(ndx));
VEC(ndx)
end
end
but they dont include the possibilities where the numbers repeat.
I found a better approach through recursion and it's more elegant (I like elegant) and faster than my previous attempt (0.00399705213 seconds on my computer).
EDIT: You will need my custom function stretchmat.m that stretches a vector to fit the size of another matrix. Kinda like repmat but stretching the first parameter (see help for details). Very useful!
script.m
% Define funciton to prepend a cell x with a variable i
cellprepend = #(x,i) {[i x]};
% Execute and time function
tic;
a = allcomb(cellprepend,1,8); % Solution in a
toc;
allcomb.m
function a = allcomb( cellprepend, m, n )
% Add entire block as a combination
a{1} = n;
% Exit recursion if block size 1
if n == 1
return;
end
% Recurse cutting blocks at different segments
for i = m:n/2
b = allcomb(cellprepend,i,n-i);
a = [a cellfun( cellprepend, b, num2cell( stretchmat( i, b ) ) )];
end
end
So the idea is simple, for solutions that add to 8 is exhaustive. If you look for only valid answers, you can do a depth first search by breaking up the problem into 2 blocks. This can be written recursively as I did above and is kinda similar to Merge Sort. The allcomb call takes the block size (n) and finds all the ways of breaking it up into smaller pieces.
We want non-zero pieces so we loop it from 1:n-1. It then prepends the first block to all the combinations of the second block. By only doing all comb on one of the blocks, we can ensure that all solutions are unique.
As for the sorting, I'm not quite sure what you mean by ascending. From what I see, you appear to be sorting from the last number in ascending order. Can you confirm? Any sort can be appended to the end of script.m.
EDIT 2/3 Notes
For the permutatively unique case, the code can be found here
Thanks to #Simon for helping me QA the code multiple times
EDIT: Look at my second more efficient answer!
The Naive approach! Where the cartprod.m function can be found here.
% Create all permutations
p(1:8) = {0:8};
M = fliplr( cartprod( p{:} ) );
% Check sums
r = sum( M, 2 ) == 8;
M = M(sum( M, 2 ) == 8,:); % Solution here
There are definitely more efficient solutions than this but if you just need a quick and dirty solution for small permutations, this will work. Please note that this made Matlab take 3.5 GB of RAM to temporarily store the permutations.
First save all combinations with repetitions in a cell array. In order to do that, just use nmultichoosek.
v = 1 : 8;
combs = cell(length(v),0);
for i = v
combs{i} = nmultichoosek(v,i);
end
In this way, each element of combs contains a matrix where each row is a combination. For instance, the i-th row of combs{4} is a combination of four numbers.
Now you need to check the sum. In order to do that to all the combinations, use cellfun
sums = cellfun(#(x)sum(x,2),combs,'UniformOutput',false);
sums contains the vectors with the sum of all combinations. For
instance, sums{4} has the sum of the number in combination combs{4}.
The next step is check for the fixed sum.
fixed_sum = 10;
indices = cellfun(#(x)x==fixed_sum,sums,'UniformOutput',false);
indices contains arrays of logical values, telling if the combination satisfies the fixed sum. For instance, indices{4}(1) tells you if the first combination with 4 numbers sums to fixed_sum.
Finally, retrieve all valid combinations in a new cell array, sorting them at the same time.
valid_combs = cell(length(v),0);
for i = v
idx = indices{i};
c = combs{i};
valid_combs{i} = sortrows(c(idx,:));
end
valid_combs is a cell similar to combs, but with only combinations that sum up to your desired value, and sorted by the number of numbers used: valid_combs{1} has all valid combinations with 1 number, valid_combs{2} with 2 numbers, and so on. Also, thanks to sortrows, combinations with the same amount of numbers are also sorted. For instance, if fixed_sum = 10 then valid_combs{8} is
1 1 1 1 1 1 1 3
1 1 1 1 1 1 2 2
This code is quite efficient, on my very old laptop I am able to run it in 0.016947 seconds.

for loop in matlab

Hi I have the following code which I believe have indexed wrongly and so Im not getting the answer I am looking for
Diesel_matrix = xlsread('test_diesel.xlsx','Sheet2');
Diesel_supply = Diesel_matrix(:,1); % Power output of diesel generator
hourly_cost = Diesel_matrix(:,2); % Diesel cost of running genreator at that output
for z = 1:21
A = [-PV_supply -WT_supply -Diesel_supply(z)*ones(24,1)];
f = [CRF_PV*CC_PV; CRF_WT*CC_WT; (CRF_Diesel_generator*CC_Diesel)+sum(hourly_cost(1:z))] ;
b = -Demand;
[x,fval,exitflag] = linprog(f,A,b,[],[],lb,ub)
end
I am trying to loop only for the third column of matrix A.
I would like to loop for all the rows in "Diesel_supply" per row of matrix A
at the moment, the code works for 21 sets of x outputs but column 3 is either row 1,2,3 etc up to row 21 of "Diesel_supply". Wheras I am trying to get it for row 1 and 2 and 3 and 4 etc up to row 21 of "Diesel_supply".
This will allow me to examine all the elements in "Diesel_Supply"
Per the conversation #user643469 and I had in chat (see link in comments section) and looking at the documentation for linprog afterwards, I think you need to store the results of each z-iteration in a data structure and then pick the best one after the loop has finished.
As I understand, the generator has 21 different modes you can run it in and it is subject to 24 different constraints. Each mode changes the constaints a little.
Instead of
[x,fval,exitflag] = linprog(f,A,b,[],[],lb,ub)
use
val = linprog(f,A,b,[],[],lb,ub)
results(z) = val;
After the loop has finished, you will be left with a results matrix with the dimensions 4x21 where the first column contains x-values, second contains fval values and third contains exitflag values. You can then you through this 'results' matrix to determine which of the 21 modes you have available to run the generator in.

Matlab Fill previous value if missing value (or zero)

I have a vector containing a time series with different values and some missing values inbetween that are set to zero:
X=[0,0,2,0,5,0,0,0,4,0];
I want to create a new vector where the missing values (zeros) are populated by the previous value if one exist so that I get a new vector looking like:
Z=[0,0,2,2,5,5,5,5,4,4];
I have been browsing through the Matlab help and forums like this to find a neat and suitable function that would solve this for me with a one line solution or similar, but I have failed to do so. I can solve the problem through a few different steps according to below but I am guessing that there must be a better and easier solution available?
Current solution:
X=[0,0,2,0,5,0,0,0,4,0];
ix=logical(X);
Y = X(ix);
ixc=cumsum(ix);
Z=[zeros(1,sum(~logical(ixc))) Y(ixc(logical(ixc)))];
This does the trick, but it seems like an overly complicated solution to a simple problem, so can anyone help me with a better one? Thanks.
Here's a somewhat simpler version using cumsum:
X=[0,0,2,0,5,0,0,0,4,0];
%# find the entries where X is different from zero
id = find(X);
%# If we want to run cumsum on X directly, we'd
%# have the problem that the non-zero entry to the left
%# be added to subsequent non-zero entries. Thus,
%# subtract the non-zero entries from their neighbor
%# to the right
X(id(2:end)) = X(id(2:end)) - X(id(1:end-1));
%# run cumsum to fill in values from the left
Y = cumsum(X)
Y =
0 0 2 2 5 5 5 5 4 4
Here's a little something I wrote up. Does this do the trick?
% INPUT: the array you would like to populate
% OUTPUT: the populated array
function popArray = populate(array)
popArray = array;
% Loops through all the array elements and if it equals zero, replaces it
% with the previous element
%
% Since there is no element before the first to potentially populate it, this
% starts with the second element.
for ii = 2:length(popArray)
if array(ii) == 0;
popArray(ii)= popArray(ii-1);
end
end
disp(popArray);
Let me suggest another vectorized solution (though I like the one by #Jonas better):
X = [0 0 2 0 5 0 0 0 4 0]
id = find(X);
X(id(1):end) = cell2mat( arrayfun(#(a,b)a(ones(1,b)), ...
X(id), [diff(id) numel(X)-id(end)+1], 'UniformOutput',false) )