i'm looking for a way to sum all adjacent elements in a matrix with 1's and 0's. so [ 1 0 1; 1 0 0; 0 0 0] would give the following matrix: [ 1 3 0; 1 3 1; 1 1 0]. for n x n matrices there already was a solution: conv2(MyMatrix, ones(n), 'same'). i'm very curious if this also works with m x n matrices (m ~= n) and how it works. Thanks!
MATLAB: How to sum all adjacent elements in a matrix with 1’s and 0’s
conv2matrix
Related Solutions
Easy enough.
matno = 10; % the number of random possiblities
rows=10;columns=3;
Now we want to create a matrix of size (rows,columns,matno), such that each row has exactly one element that is 1, and the placement of that 1 is random. This is most simply done by creating a matrix of size (matno*rows,columns), where each of those rows has exactly one true element.
The location of those 1's will be:
cloc = randi(columns,[matno*rows,1]);matrices = zeros(matno*rows,columns);matrices(sub2ind([matno*rows,columns],(1:(matno*rows))',cloc)) = 1;matrices = permute(reshape(matrices,matno,rows,columns),[2 3 1]);matricesmatrices(:,:,1) = 0 0 1 0 0 1 1 0 0 0 1 0 0 1 0 1 0 0 1 0 0 0 0 1 0 0 1 1 0 0matrices(:,:,2) = 0 1 0 1 0 0 1 0 0 0 0 1 1 0 0 0 0 1 1 0 0 0 1 0 1 0 0 0 0 1matrices(:,:,3) = 1 0 0 1 0 0 1 0 0 0 1 0 0 1 0 1 0 0 1 0 0 0 1 0 0 0 1 1 0 0matrices(:,:,4) = 0 1 0 0 0 1 0 1 0 0 1 0 0 0 1 0 0 1 0 1 0 0 1 0 0 1 0 1 0 0matrices(:,:,5) = 1 0 0 1 0 0 0 1 0 0 0 1 1 0 0 1 0 0 0 0 1 0 1 0 0 1 0 0 1 0matrices(:,:,6) = 0 0 1 0 0 1 1 0 0 1 0 0 0 0 1 0 1 0 0 1 0 1 0 0 0 0 1 0 1 0matrices(:,:,7) = 1 0 0 0 0 1 1 0 0 0 1 0 0 0 1 1 0 0 0 0 1 0 0 1 0 1 0 1 0 0matrices(:,:,8) = 0 0 1 1 0 0 0 1 0 0 0 1 1 0 0 0 1 0 1 0 0 0 1 0 0 1 0 0 0 1matrices(:,:,9) = 0 0 1 1 0 0 0 0 1 1 0 0 1 0 0 0 1 0 0 1 0 0 0 1 0 0 1 1 0 0matrices(:,:,10) = 1 0 0 1 0 0 1 0 0 0 0 1 0 1 0 0 1 0 1 0 0 0 1 0 1 0 0 0 1 0
Easy. Almost trivial. To prove that the result has exactly one 1 in each row, this next result should be a 10x10 array of 1's.
squeeze(sum(matrices,2))ans = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
As it is.
Structurally, your code needs to look more like the following:
T_span_MSK = linspace(460,860)';% E_MSK = []; %matrix of acitvation energies for pure MSK (primary, secondary, and teriary degradations respectively)
% n_MSK = []; %matrix of reaction orders for pure MSK
% A_MSK =[]; %matrix of preexponential factors for pure MSK
E_MSK(1) = 8.309;n_MSK(1) =3;A_MSK(1) = 9.76*10^(-2);E_MSK(2) = 37.832;n_MSK(2) = 0.9;A_MSK(2) = 3.64*10^2;E_MSK(3)=14.99;n_MSK(3) = 1;A_MSK(3) = 1.61;[T_MSK,x_MSK] = ode45(@(T_MSK,x_MSK) MSK_pyrolysis(T_MSK,x_MSK,A_MSK,E_MSK,n_MSK), T_span_MSK,0); function dxdT_MSK =MSK_pyrolysis(T_MSK,x_MSK, A_MSK,E_MSK,n_MSK) R = 8.3145; % J mol^-1 K^-1
if T_MSK < 500 dxdT_MSK = A_MSK(1).*exp(-E_MSK(1)./(R.*T_MSK)).*(1-x_MSK)^n_MSK(1); elseif (T_MSK >= 500) && (T_MSK <620) dxdT_MSK = A_MSK(2).*exp(-E_MSK(2)./(R.*T_MSK)).*(1-x_MSK)^n_MSK(2); elseif (T_MSK >= 620) && (T_MSK <690) dxdT_MSK=A_MSK(3).*exp(-E_MSK(3)./(R.*T_MSK)).*(1-x_MSK)^n_MSK(3); elseif T_MSK >=690 dxdT_MSK=0; end end
However,
(i) when x_MSK gets to 1 you start getting complex values because of the value of n_MSK(2).
(ii) you don't give units so I might have used the wrong value for R (which you didn't include).
(iii) you don't have a beta in your rate equations (dx.dT = A/beta*exp(-E/(RT))*(1-x)^n)- is that because that constant is wrapped up in your A values?
Best Answer