construct a matrix in Octave - matrix

I have a vector idx = [3; 5; 3; 4; 3; 2; 5; 1]. The number is from 1:k with k = 5. I want to make a "k by m" matrix A (m is the number of elements in the vector idx). Each row of A contains either '0' or '1' with '1' indicated by the index of the vector idx. For example, the third row of A (k = 3) is "1" at columns 1, 3, 5 because those are the indexes of "3" in idx. So that A =
[0 0 0 0 0 0 0 1; 0 0 0 0 1 0 0 0; 1 0 1 0 1 0 0 0; 0 0 0 1 0 0 0 0; 0 1 0 0 0 0 1 0]
How can I do this in Octave? Thank you!

Or another way:
idx = [3; 5; 3; 4; 3; 2; 5; 1];
A = sparse (idx, [1:numel(idx)], 1)
A = Compressed Column Sparse (rows = 5, cols = 8, nnz = 8 [20%])
(3, 1) -> 1
(5, 2) -> 1
(3, 3) -> 1
(4, 4) -> 1
(3, 5) -> 1
(2, 6) -> 1
(5, 7) -> 1
(1, 8) -> 1
Which gives you a compressed column sparse (very efficient), you can convert this to a "normal, full matrix":
B = full (A)
B =
0 0 0 0 0 0 0 1
0 0 0 0 0 1 0 0
1 0 1 0 1 0 0 0
0 0 0 1 0 0 0 0
0 1 0 0 0 0 1 0

Try this:
idx = [3; 5; 3; 4; 3; 2; 5; 1];
n = numel(idx);
k = 5;
A=zeros(k,n);
A(sub2ind(size(A), idx, [1:n]')) = 1
Output is:
A =
0 0 0 0 0 0 0 1
0 0 0 0 0 1 0 0
1 0 1 0 1 0 0 0
0 0 0 1 0 0 0 0
0 1 0 0 0 0 1 0

Related

Eigen Dynamic-sized matrix verticaly/horizontaly Linspaced?

I'm currently working on a project where i need to be able to create matrix such as :
MatrixXi lin_spaced_horizontaly =
0 1 2 3 4 ... ncols
0 1 2 3 4 ... ncols
0 1 2 3 4 ... ncols
MatrixXi lin_spaced_verticaly =
0 0 0
1 1 1
2 2 2
3 3 3
4 4 4
. . .
nrows nrows nrows
Currently i am trying things like that :
Eigen::VectorXi v_lin_vec = Eigen::VectorXi::LinSpaced(nrows_, 0, nrows_).transpose
Eigen::MatrixXi v_lin_matrix (nrows_, ncols_);
for (auto i = 0; i<ncols_; i++)
v_lin_matrix << v_lin_vec;
Eigen::VectorXi h_lin_vec = Eigen::VectorXi::LinSpaced(ncols_, 0, ncols_)
Eigen::MatrixXi h_lin_matrix (nrows_, ncols_);
for (auto i = 0; i<ncols_; i++)
h_lin_matrix << h_lin_vec;
And I am getting results such as :
v_lin_matrix
-------------
0 0 0 0 0
1 0 0 0 0
2 0 0 0 0
3 0 0 0 0
4 0 0 0 0
h_lin_matrix
-------------
0 0 0 0 0
1 0 0 0 0
2 0 0 0 0
3 0 0 0 0
4 0 0 0 0
Thanks in advance !
You can use .rowwise().replicate(ncols_) and .colwise().replicate(nrows_) like so:
Eigen::MatrixXi v_lin_matrix = Eigen::VectorXi::LinSpaced(nrows_, 0, nrows_)
.rowwise().replicate(ncols_);
Eigen::MatrixXi h_lin_matrix = Eigen::RowVectorXi::LinSpaced(ncols_, 0, ncols_)
.colwise().replicate(nrows_);
Or alternatively:
Eigen::MatrixXi v_lin_matrix = Eigen::VectorXi::LinSpaced(nrows_, 0, nrows_)
.replicate(1,ncols_);
Eigen::MatrixXi h_lin_matrix = Eigen::RowVectorXi::LinSpaced(ncols_, 0, ncols_)
.replicate(nrows_,1);
Regarding your use of <<: This is meant to be used in combination with the , operator to initialize a matrix in a single expression, like this:
Eigen::MatrixXi A(4,ncols_);
A << row0, row1, row2, row3;
What you wrote will assert at runtime (if you compile without -DNDEBUG, which I strongly recommend until your code is sufficiently tested!)

Adjacent Elements in MATLAB with Mathematical Formulation

I have a set with elements and the possible adjacent combinations for this are:
So the total possible combinations are c=11 which can be calculated with the formula:
I can model this using a as below whose elements can be represented as a(n,c) are:
I have tried to implement this in MATLAB, but since I have hard-coded the above math my code is not extensible for cases where n > 4:
n=4;
c=((n^2)/2)+(n/2)+1;
A=zeros(n,c);
for i=1:n
A(i,i+1)=1;
end
for i=1:n-1
A(i,n+i+1)=1;
A(i+1,n+i+1)=1;
end
for i=1:n-2
A(i,n+i+4)=1;
A(i+1,n+i+4)=1;
A(i+2,n+i+4)=1;
end
for i=1:n-3
A(i,n+i+6)=1;
A(i+1,n+i+6)=1;
A(i+2,n+i+6)=1;
A(i+3,n+i+6)=1;
end
Is there a relatively low complexity method to transform this problem in MATLAB with n number of elements of set N, following my above mathematical formulation?
The easy way to go about this is to take a bit pattern with the first k bits set and shift it down n - k times, saving each shifted column vector to the result. So, starting from
1
0
0
0
Shift 1, 2, and 3 times to get
|1 0 0 0|
|0 1 0 0|
|0 0 1 0|
|0 0 0 1|
We'll use circshift to achieve this.
function A = adjcombs(n)
c = (n^2 + n)/2 + 1; % number of combinations
A = zeros(n,c); % preallocate output array
col_idx = 1; % skip the first (all-zero) column
curr_col = zeros(n,1); % column vector containing current combination
for elem_count = 1:n
curr_col(elem_count) = 1; % add another element to our combination
for shift_count = 0:(n - elem_count)
col_idx = col_idx + 1; % increment column index
% shift the current column and insert it at the proper index
A(:,col_idx) = circshift(curr_col, shift_count);
end
end
end
Calling the function with n = 4 and 6 we get:
>> A = adjcombs(4)
A =
0 1 0 0 0 1 0 0 1 0 1
0 0 1 0 0 1 1 0 1 1 1
0 0 0 1 0 0 1 1 1 1 1
0 0 0 0 1 0 0 1 0 1 1
>> A = adjcombs(6)
A =
0 1 0 0 0 0 0 1 0 0 0 0 1 0 0 0 1 0 0 1 0 1
0 0 1 0 0 0 0 1 1 0 0 0 1 1 0 0 1 1 0 1 1 1
0 0 0 1 0 0 0 0 1 1 0 0 1 1 1 0 1 1 1 1 1 1
0 0 0 0 1 0 0 0 0 1 1 0 0 1 1 1 1 1 1 1 1 1
0 0 0 0 0 1 0 0 0 0 1 1 0 0 1 1 0 1 1 1 1 1
0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 1 0 0 1 0 1 1

Convert adjacent elements of matrix in Matlab

I'm working on Brushfire algorithm and I need to make a loop which will scan through the matrix and find the adjacent zeros with ones and convert "1" to "2". Assume that I have a matrix 5 by 5:
0 0 0 0 0
0 1 1 1 1
0 0 1 1 1
0 0 1 1 1
0 0 1 1 1
Can I somehow make it:
0 0 0 0 0
0 2 2 2 2
0 0 2 1 1
0 0 2 1 1
0 0 2 1 1
Thank you
With the image processing toolbox, the algorithm would be:
A = [0 0 0 0 0
0 1 1 1 1
0 0 1 1 1
0 0 1 1 1
0 0 1 1 1];
B = A;
%# set pixels at border between 0 and 1 to 2
B(imdilate(~A,true(3)) & A>0) = 2;
You do it with 2D-convolution, using the standard function conv2. Denoting your matrix as X,
mask = [0 1 0; 1 1 1; 0 1 0]; %// or [1 1 1; 1 1 1; 1 1 1] to include diagonal adjacency
X(conv2(double(~X), mask, 'same') & X) = 2;

Matlab file input

I am asking this as a person with no experience in Matlab.
The project is simple; write a code to analyse a simple truss. The following is what a team mate has been working on - I'm supposed to write the input file:
file = input('File Input): ', 's');
run(file)
%Cc corresponds to # of joints and Rc corresponds to # of members
[Rc Cc] = size(C);
%JM is a matrix that contains the joint numbers that each member is
%connected to. The row number is the member number
JM = zeros(Cc, 2);
%x components
Ax = zeros(Rc, Cc);
%y components
Ay = zeros(Rc, Cc);
My_total_length = 0;
Member_length = 0;
%finding JM matrix which stores the joints that each member is connected to
%loop through C matrix and store the joint number each member is connected
%to
for j = 1:Cc
counter = 0;
for i = 1:Rc
if C(i,j) == 1
counter = counter + 1;
JM(j, counter) = i;
end
end
end
%using JM, loop through C again and create the equilibrium equations using
%the locations of each joint X and Y
for j = 1:Cc
counter = 3;
for i = 1:Rc
if C(i,j) == 1
%use counter to get x2 - x1 from JM vector
counter = counter - 1;
%find x and y distance of member and divide it by length of the member
Member_length = sqrt(((X(JM(j,2))) - ...
(X(JM(j,1))))^2 + ((Y(JM(j,2))) - (Y(JM(j,1))))^2);
Ax(i,j) = (X(JM(j,counter)) - X(i)) / Member_length; ! 13!
Ay(i,j)= (Y(JM(j, counter)) - Y(i)) / Member_length;
end
end
My_total_length = My_total_length + Member_length;
end
%combine the 4 matrices to create the larger matrix A
A = [Ax Sx; Ay Sy];
%define the vector for the forces on each member
T = zeros(Cc +3, 1);
T = inv(A)*-L;
%OUTPUT
%Find what the load on the truss is by looping through the weight vector and finding the load
for i=1:length(L)
if L(i) ~= 0
Load = L(i);
end
end
%Display load
fprintf('Load: %f N \n',Load)
%Display the forces on the members. Loop through Vector T up to the where reaction forces are
fprintf('Member forces in Newtons: \n')
for i = 1:length(T)-3
if (T(i)<0)
fprintf('m%d: %2.3f (C) \n', i, abs(T(i)))
elseif (T(i)==0)
fprintf('m%d: %d \n', i, abs(T(i)))
else
fprintf('m%d: %2.3f (T) \n', i, abs(T(i)))
end
end
%Display the last three items of vector T, the reaction forces
fprintf('Reaction forces in Newtons: \nSx1: %2.3f \nSy1: %2.3f \nSy2: %2.3f \n'...
, T(length(T)-2), T(length(T)-1), T(end))
%Calculate cost of truss
Cost = Rc*(10) + My_total_length*(1);
%Display the cost of truss
fprintf('Cost of truss: $%3.2f \n', Cost)
%Display load/cost ratio
fprintf('Theoretical max load/cost ratio in N/$: %2.4f \n', (Load/Cost))
Now, the problem I have is actually the input file. What kind of file should I save it as? How do I write it in the file? When I'm trying to run the code and need to access the file, how do I write the file name (with .txt after)? Some of the information that has to go into the file is below:
Sx = [ 1 0 0; 0 0 0; 0 0 0; 0 0 0; 0 0 0; 0 0 0; 0 0 0; 0 0 0; 0 0 0, 0 0 0; 0 0 0]
Sy = [ 0 1 0; 0 0 0; 0 0 0; 0 0 0; 0 0 0; 0 0 0; 0 0 0; 0 0 0; 0 0 0; 0 0 0; 0 0 1]
C = [ 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0; 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ; 0 1 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0; 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0; 0 0 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 0 0 1 1 0 1 1 0 0 0 0 0; 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 1 0 0 0; 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 1 0; 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1; 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1]
L = [ 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 500; 0; 0; 0; 0; 0]
X = [ 0,6,12,18,24,30,36,42,48,54,60]
Y = [ 0,8,0,8,0,8,0,8,0,8,0]
Would truly appreciate any help.
You can use fwrite to write to a txt file. This does however need fread and so code to read it into matlab again. xlswrite and xlsread does the same for xls files.
However, the simplest way if a file only is to be used together with matlab is to use the functions load and save. You can read the help for them with help save or help load, but the can save one or more variables to a .mat file.
a = 1;
b = 'e';
c = [1,2;3,4];
d = struct('hi',1,'you','e');
save('myFileName.mat','a','b','c','d');
clear;
load('myFileName.mat')
You can also write filepath/filename (or \ for windows) where filepath is the full path.

I want replace the values of 1 in an adjacency matrix with weights given in another smaller matrix

How can I replace the values of 1 in an adjacency matrix with weights given in another matrix?
For example:
adjacent_matrix = [1 0 0 1; 0 0 1 1; 1 0 1 0; 0 1 1 0 ]
weight_matrix = [ 2 4 6 2; 4 5 1 3]
The final matrix should look like this: [2 0 0 4; 0 0 6 2; 4 0 5 0; 0 1 3 0]
Code -
out = adjacent_matrix';
out(out==1) = reshape(weight_matrix',1,numel(weight_matrix))';
out = out';
Inputs 'adjacent_matrix' and 'weight_matrix' stay the same, as suggested by #chappjc.
accumarray solution:
>> [ii,jj] = find(adjacent_matrix.');
>> out = accumarray([ii jj],reshape(weight_matrix.',[],1)).'
out =
2 0 0 4
0 0 6 2
4 0 5 0
0 1 3 0
sparse solution:
[ii,jj] = find(adjacent_matrix.');
out = full(sparse(ii,jj,weight_matrix.')).'

Resources