Answered
How to set Variable-step in ODE15s?
[t,N]=ode15s(odefun,[0:5:2000],NN0); Don't be confuse, the vector in tspan does not specify the solver step, just the solut...

7 years ago | 1

Answered
Cumulative sum with conditions
Change the last line to make it can handle with single row: m = size(h,1); d = diff(h,1,2); [r,c] = find(d==-1...

7 years ago | 0

| accepted

Answered
Cumulative sum with conditions
m = size(h,1); d = diff(h,1,2); [r,c] = find(d==-1); b = sum(abs(d),2)==1; s = accumarray(r(:),c(:),[m 1]).*doubl...

7 years ago | 0

Answered
Cumulative sum with conditions
h = [1 0 0; 1 1 0; 0 0 1; 1 1 1; 0 1 0]; [a,c] = max(1-h,[],2); s = (c-1).*a.*h(:,1)

7 years ago | 0

Answered
Calculate median with accumarray
Internally the median command "sort" input data and take a middle element of the sorted array (or average of two left and right ...

7 years ago | 1

Answered
Help solving using ode45
%main script close all clear k=8500; m1=2650; m2=1830; [t,xy] = ode45(@(t,xy) xyder(t,xy,k,m1,m2), [...

7 years ago | 1

| accepted

Answered
finde some maximum values of matrix without collision!!!!
A = ceil(5*rand(4,6)) B = A; m = size(B,1); c = zeros(m,1); maxA = zeros(m,1); r = (1:m)'; for i=1:m ...

7 years ago | 0

Answered
How to replace k-th diagonal by vector?
You can use the function IDIAG in this <https://fr.mathworks.com/matlabcentral/fileexchange/23391-triangular-and-diagonal-indexi...

7 years ago | 2

Answered
interesting matrix indexing question without for loops
C = A(B+reshape(0:numel(B)-1,size(B))*size(A,1))

7 years ago | 0

Answered
elimination of conscutive regions (generalization: ones with zeros between)
That's true, somehow it's a 1D scanning problem. I think we are close to the limit of MATLAB can do, if faster speed is still...

7 years ago | 0

Answered
how to share matlab functions without code
help pcode you might also put all the subfunctions in a single file together with the main file. Admittedly this would be a...

7 years ago | 0

Answered
What is the simplest way to extract lengths of NaN sequences from a vector?
% a is the input vector b = isnan(a); counts = sum(b); positions = find(b);

7 years ago | 1

Answered
elimination of conscutive regions (generalization: ones with zeros between)
A = [1 -1 0; 0 1 1; 0 1 1; 1 1 0; 0 0 1; 1 -...

7 years ago | 0

| accepted

Answered
How to multiply efficiently a vector by a matrix with only zeros and ones?
You should consider storing A in SPARSE in not already done. I don't think avoid multiplication would have much impact.

7 years ago | 0

Answered
Sort an array logical like a stair
If you have Image Proc TBX, you can use watershed regionprops on the logical array isnan(A)

7 years ago | 0

Answered
How can I select a random element from each column of an array, ignoring zeros
B = A; B(B==0) = NaN; B = sort(B,1); [~,rlast] = max(B,[],1); rrand = ceil(rand(size(rlast)).*rlast); rval = B(...

7 years ago | 2

| accepted

Answered
Speed up code on GPU by removing nested for loops
f1 = exp((1i*pi) * (ii(:) + jj(:).')); or on older MATLAB version (without auto expansion). f1 = exp(bsxfun(@plus, ...

7 years ago | 0

| accepted

Answered
Performance: for each element with value '1', find amount of elements with value '2' after it
a = [0 0 1 0 0 1 2 2 1 0 2] fa = flip(a); c = cumsum(fa==2); b = flip(c(fa==1)) b = 3 3 1

7 years ago | 1

| accepted

Answered
Efficiently repeating nullspace operation
Here is one more method, so far fastest and derived from a much more direct calculation. The idea is like this : For a fix ma...

7 years ago | 1

| accepted

Answered
Problem with plotting - legend
You probably have more than one curves in each color hdata = plot(T3_All(mask),Tmax_All(mask),'o'); hlin = plot(T3_All,...

7 years ago | 0

| accepted

Answered
Using "isfinite" to skip NAN data
isvalid = isfinite(T3_All) & isfinite(Tmax_All); coeff1 = polyfit(T3_All(isvalid),Tmax_All(isvalid),1) You would recogni...

7 years ago | 0

| accepted

Answered
Efficiently repeating nullspace operation
Here we go, a vectorized Housholder QR. You might need to replace the auto-expension with bsxfun if you run old MATLAB version. ...

7 years ago | 1

Answered
problem figuring out 2 different solutions
if t<=s1(1,n) w = ((2695-6169)/(32.2-0))*dt + 6169; elseif t>s2(1,n) ...

7 years ago | 0

| accepted

Answered
2 coupled second order equations
Bellow is the code with 2 methods. Observation: piecewise RUNGE-KUTTA method is more accurate whenever the trajectory is orbi...

7 years ago | 0

| accepted

Answered
Efficiently repeating nullspace operation
Faster method still, solve a linear system by introducing an arbitrary vectors. It needs such multiple system solver such as ...

7 years ago | 2

Answered
How to create N-tuples in Matlab?
dec2bin(0:2^3-1)-'0'

7 years ago | 1

| accepted

Answered
Polynomial using linear least squares fitting
Here is the code of fitting a (complex) polynomial to a (complex) data by imposing one root. It can be extended to more roots i...

7 years ago | 0

| accepted

Answered
Test value equality within a range of values
Is this is what you want? jki = 0:0.00065:0.65; a = 0.3; [~,loc] = histc(a,jki); fprintf('%g is in the range (...

7 years ago | 0

Answered
Test for integer value
according to my test slightly faster, but need to assume the smaller range of x of be working: x == int32(x)

7 years ago | 0

Answered
Test for integer value
Perhaps no better no worse than James's solution just different iswholenumber = mod(x,1)==0

7 years ago | 0

Load more