Answered

Best practices to use Matlab assert

No C debug mode slows every statement down. ASSERT costs the overhead of checking whatever you instruct MATLAB to check. It ma...

Best practices to use Matlab assert

No C debug mode slows every statement down. ASSERT costs the overhead of checking whatever you instruct MATLAB to check. It ma...

4 månader ago | 0

Answered

Program to generate permutations in a certain order

clear X X{1}=[1 1 0 1]; X{2}=[1 0 1 0]; X{3}=[1 0 1 1]; X{4}=[1 1 1 0]; X{5}=[0 1 0 1]; X{6}=[0 0 0 1]; n=length(X); P...

Program to generate permutations in a certain order

clear X X{1}=[1 1 0 1]; X{2}=[1 0 1 0]; X{3}=[1 0 1 1]; X{4}=[1 1 1 0]; X{5}=[0 1 0 1]; X{6}=[0 0 0 1]; n=length(X); P...

4 månader ago | 1

Answered

Equivalent of c++'s NULL or python' s None in MATLAB

If you use recent MATLAB you can use also missing and ismissing

Equivalent of c++'s NULL or python' s None in MATLAB

If you use recent MATLAB you can use also missing and ismissing

4 månader ago | 0

Question

How to change number precision with writestruct

The number of significant digits of writestruct is quite limited, as showed by this example s=struct('pi',pi); writestruct(s,...

4 månader ago | 1 answer | 0

Answered

Replace nested loops with a matrix

i=2:nhx-1; j=2:nhy-1; U(i,j)= U(i,j)-dt*(Pc(i+1,j)-Pc(i-1,j))/(2*hx);

Replace nested loops with a matrix

i=2:nhx-1; j=2:nhy-1; U(i,j)= U(i,j)-dt*(Pc(i+1,j)-Pc(i-1,j))/(2*hx);

5 månader ago | 1

| accepted

Answered

Get indices of all rows containing a value - repeated or not

[c,~,J] = unique(sort(connections,2),'rows'); [i,~,I] = unique(c); [~,j] = ismember(i, id); xy = [x(j) y(j)]; I = reshape(I,...

Get indices of all rows containing a value - repeated or not

[c,~,J] = unique(sort(connections,2),'rows'); [i,~,I] = unique(c); [~,j] = ismember(i, id); xy = [x(j) y(j)]; I = reshape(I,...

5 månader ago | 0

| accepted

Answered

Is there a way to vectorise this vector/matrix product?

G = real(dot(W,R*W,1)).';

Is there a way to vectorise this vector/matrix product?

G = real(dot(W,R*W,1)).';

5 månader ago | 0

Answered

Relative Ranking of position in an array

I10=[1 2 1 1 1 2 1 1 2 2 1 3 6 3 1 3 3 1 4 3 4 3 2 3 4 2 3 3 4 2 7 4 6 2 5 4 5 5 5 5 6 7 5 4 5 8 8 6 4 5 4 6 6 7 9 7 7 2 7...

Relative Ranking of position in an array

I10=[1 2 1 1 1 2 1 1 2 2 1 3 6 3 1 3 3 1 4 3 4 3 2 3 4 2 3 3 4 2 7 4 6 2 5 4 5 5 5 5 6 7 5 4 5 8 8 6 4 5 4 6 6 7 9 7 7 2 7...

5 månader ago | 0

Answered

Optimize repeated permutation of a large vector

s = [1 2 3 4 5 6 7 8]; n=[4 3 1]; c=cumsum([0 n]); p=2:length(s)+1; p(c(2:end))=c(1:end-1)+1; s(p)

Optimize repeated permutation of a large vector

s = [1 2 3 4 5 6 7 8]; n=[4 3 1]; c=cumsum([0 n]); p=2:length(s)+1; p(c(2:end))=c(1:end-1)+1; s(p)

5 månader ago | 0

Answered

Error using fwrite : Cannot write value: sparse arrays are unsupported.

[i,j,v] = find(S); then save i,j,v with fwrite

Error using fwrite : Cannot write value: sparse arrays are unsupported.

[i,j,v] = find(S); then save i,j,v with fwrite

5 månader ago | 0

| accepted

Answered

Optimize repeated permutation of a large vector

s = [1 2 3 4 5 6 7 8]; i=1:length(s); n=[4 3 1]; c=cumsum([1 n]); b=cumsum(ismember(i,c)); p=mod(i+1-c(b),n(b))+c(b) s(p...

Optimize repeated permutation of a large vector

s = [1 2 3 4 5 6 7 8]; i=1:length(s); n=[4 3 1]; c=cumsum([1 n]); b=cumsum(ismember(i,c)); p=mod(i+1-c(b),n(b))+c(b) s(p...

5 månader ago | 0

| accepted

Answered

find the position of all non-zero minimum values in each column of a matrix

a = [2 0 3;1 2 5;0 0 0;1 3 3;0 0 6;0 2 7;1 0 0] b=a; b(b==0)=NaN; [r,c]=find(b==min(b,[],1)); rmin=accumarray(c(:),r(:),[siz...

find the position of all non-zero minimum values in each column of a matrix

a = [2 0 3;1 2 5;0 0 0;1 3 3;0 0 6;0 2 7;1 0 0] b=a; b(b==0)=NaN; [r,c]=find(b==min(b,[],1)); rmin=accumarray(c(:),r(:),[siz...

5 månader ago | 1

Answered

Turn logical matrix into string vector

Assuming idx has one 1 per row idx = logical([0 1 0; 0 0 1; 1 0 0; 1 0 0; 0 1 0]); vec = ["A","B","C"]; [r,c]=find(idx); r...

Turn logical matrix into string vector

Assuming idx has one 1 per row idx = logical([0 1 0; 0 0 1; 1 0 0; 1 0 0; 0 1 0]); vec = ["A","B","C"]; [r,c]=find(idx); r...

5 månader ago | 1

Answered

Symetric matrix power optimization

Youeu can edit MATLAB function mpower.m and try to the basic calculation (line 76 in my case) D = D*D; % I assume p is integer...

Symetric matrix power optimization

Youeu can edit MATLAB function mpower.m and try to the basic calculation (line 76 in my case) D = D*D; % I assume p is integer...

5 månader ago | 0

| accepted

Answered

How can I avoid loop for code optimization?

Use MultipleQR FEX (C compiler for MEX build is required, unless for Windows platform) y = linspace(1,10,1000); Y = reshape(...

How can I avoid loop for code optimization?

Use MultipleQR FEX (C compiler for MEX build is required, unless for Windows platform) y = linspace(1,10,1000); Y = reshape(...

5 månader ago | 1

Answered

random number of two random varibles.

If K is the (2 x 2) covariariance matrix of (w,T) and mu (2 x 1) is the mean of (w,T), you can generate them like this n = 100...

random number of two random varibles.

If K is the (2 x 2) covariariance matrix of (w,T) and mu (2 x 1) is the mean of (w,T), you can generate them like this n = 100...

5 månader ago | 0

Answered

How to avoid a negative solution with fmincon, including an external equation?

The condition f2 = - 2*x1 - 10*x2 + 30 >= 0 is equivalen to 2*x1 + 10*x2 + 0*f1 <= 30. So set A = [2 10 0] b = 30 If you ...

How to avoid a negative solution with fmincon, including an external equation?

The condition f2 = - 2*x1 - 10*x2 + 30 >= 0 is equivalen to 2*x1 + 10*x2 + 0*f1 <= 30. So set A = [2 10 0] b = 30 If you ...

5 månader ago | 0

| accepted

Answered

How can I optimize coefficients of a polynomial using fmincon?

"The overall goal is to minimize the length of the cost function " The overall length is L = integral sqrt(dx/dt^2+dy/dt^2+dy/...

How can I optimize coefficients of a polynomial using fmincon?

"The overall goal is to minimize the length of the cost function " The overall length is L = integral sqrt(dx/dt^2+dy/dt^2+dy/...

5 månader ago | 0

Answered

Is it possible to vectorize this?

NOTE the order of most is sorted with this algorithm: A = [1 2 0 3 4 6; 9 3 4 0 9 5; 4 3 0 5 6 7; 3 7 7 3 0 0...

Is it possible to vectorize this?

NOTE the order of most is sorted with this algorithm: A = [1 2 0 3 4 6; 9 3 4 0 9 5; 4 3 0 5 6 7; 3 7 7 3 0 0...

5 månader ago | 2

Answered

Diagonal of non-square matrix

n = 8; A = spdiags([2 3 4]+zeros(n,1),0:2,n,n+2); A = full(A) % if prefered

Diagonal of non-square matrix

n = 8; A = spdiags([2 3 4]+zeros(n,1),0:2,n,n+2); A = full(A) % if prefered

5 månader ago | 1

Answered

Randomly splitting of a number in a sum format.

This code will generate "uniform" partition distribution, in the sense that all possible partition has equal probability: n = 1...

Randomly splitting of a number in a sum format.

This code will generate "uniform" partition distribution, in the sense that all possible partition has equal probability: n = 1...

5 månader ago | 1

Answered

Will codegen accelerate sub-functions?

Yes coder translates the sub-functions to C as well.

Will codegen accelerate sub-functions?

Yes coder translates the sub-functions to C as well.

5 månader ago | 0

| accepted

Answered

Randomly splitting of a number in a sum format.

n = 10; for j=1:10 r = n; i = 1; clear s while r > 0 s(i) = ceil(r*rand); r = r-s(i); ...

Randomly splitting of a number in a sum format.

n = 10; for j=1:10 r = n; i = 1; clear s while r > 0 s(i) = ceil(r*rand); r = r-s(i); ...

5 månader ago | 0

| accepted

Answered

mldivide algorithm for an underdetermined system of equations

The outline of "\" goes as following: % Test data A=randi(10,3,2)*randi(10,2,5) b=randi(10,3,1) [Q, R, P] = qr(A,'vector')...

mldivide algorithm for an underdetermined system of equations

The outline of "\" goes as following: % Test data A=randi(10,3,2)*randi(10,2,5) b=randi(10,3,1) [Q, R, P] = qr(A,'vector')...

5 månader ago | 2

| accepted

Answered

Function to create struct with parameters

s = buildparams() function s = buildparams() a = 1; b = 2; s = gathervars(); end function s = gathervars() vars = eva...

Function to create struct with parameters

s = buildparams() function s = buildparams() a = 1; b = 2; s = gathervars(); end function s = gathervars() vars = eva...

5 månader ago | 0

Submitted

nchoosek_enum

nchoosek with enumerate selection. This function never build the full combination array in memory

5 månader ago | 1 download |

Answered

Creating separate combinations with repetition

I program a function nchoosek_enum that is almost like nchoosek, excepted that you can pass an enumerated array i that selects t...

Creating separate combinations with repetition

I program a function nchoosek_enum that is almost like nchoosek, excepted that you can pass an enumerated array i that selects t...

5 månader ago | 0

Answered

How to get all combinations from this matrix?

ch=['MRWI' 'IDMZ' 'CGBI' 'JKNO']; c=num2cell(ch,2); [c{:}]=ndgrid(c{:}); n=size(c,1); c = reshape(cat(n+1,c{:}),[],n)

How to get all combinations from this matrix?

ch=['MRWI' 'IDMZ' 'CGBI' 'JKNO']; c=num2cell(ch,2); [c{:}]=ndgrid(c{:}); n=size(c,1); c = reshape(cat(n+1,c{:}),[],n)

5 månader ago | 0

| accepted

Answered

How to trim the non-manifold triangle off the stl file?

There is SW that can detect topology issue and attempt to correct it, such as https://www.meshlab.net/

How to trim the non-manifold triangle off the stl file?

There is SW that can detect topology issue and attempt to correct it, such as https://www.meshlab.net/

5 månader ago | 0

Answered

how to count the number of occurrences of these numbers (with some conditions)

A(1,:) = [1, 2, 5, 2 , 1 , 2]; A(2,:) = [0,1,1,0,1,0]; [B,~,J] = unique(A(1,:)); N = accumarray(J(:),A(2,:)'); B = B'; ta...

how to count the number of occurrences of these numbers (with some conditions)

A(1,:) = [1, 2, 5, 2 , 1 , 2]; A(2,:) = [0,1,1,0,1,0]; [B,~,J] = unique(A(1,:)); N = accumarray(J(:),A(2,:)'); B = B'; ta...

5 månader ago | 0

| accepted