I have been using Matlab since version 4 (1999 or so) and still prefer it over all other software tools available for data manipulation :-)

My professional interests: neuroscience (motor control & eye movements), cognitive psychology physics, mathematics, teaching

"The most important part of programming is writing the comments!"

Answered

for loop taking too long to operate

Pre-allocation, and recoding using logical indexing and sum should speed things up popn1 = nan(300,1) ; % pre-allocation for i...

for loop taking too long to operate

Pre-allocation, and recoding using logical indexing and sum should speed things up popn1 = nan(300,1) ; % pre-allocation for i...

6 mois ago | 1

| accepted

Answered

How to make columns the same size

A solution: A = [1 3 ; 2 4 ; 5 7] B = [10 11 12 ; 13 14 15] newB = repmat(mean(A,2), 1, size(B,2)) newB(1:size(B,1),:) = B

How to make columns the same size

A solution: A = [1 3 ; 2 4 ; 5 7] B = [10 11 12 ; 13 14 15] newB = repmat(mean(A,2), 1, size(B,2)) newB(1:size(B,1),:) = B

environ un an ago | 0

| accepted

Answered

How can I search a cell array for all instances of a character and replace it with something else?

Something along these lines, perhaps? C = {1.1 pi '*' ; 3 '*' 2.2} tf = cellfun(@(x) isequal(x,'*'), C) idx = find(tf)

How can I search a cell array for all instances of a character and replace it with something else?

Something along these lines, perhaps? C = {1.1 pi '*' ; 3 '*' 2.2} tf = cellfun(@(x) isequal(x,'*'), C) idx = find(tf)

plus d'un an ago | 0

Answered

If a number in a matrix is <9 add 5 to it

Use the same selection on the right hand side of the equal sign A(A<9) = A(A<9) + 5 I myself prefer to code it like this tf =...

If a number in a matrix is <9 add 5 to it

Use the same selection on the right hand side of the equal sign A(A<9) = A(A<9) + 5 I myself prefer to code it like this tf =...

plus d'un an ago | 0

| accepted

Answered

How to set the diagonal of a cell array of matrices?

Well found, Mohammed! You should put as an answer here :-) Here is another method A = cell(4,4) % a cell array n = size(A,1)...

How to set the diagonal of a cell array of matrices?

Well found, Mohammed! You should put as an answer here :-) Here is another method A = cell(4,4) % a cell array n = size(A,1)...

plus d'un an ago | 0

| accepted

Answered

how to add a row and column to a matrix ?

One easy option is to do this for rows and columns separately A = [1 2 3 ; 4 5 6 ; 7 8 9] x = 3 ; % add a row/column of ones b...

how to add a row and column to a matrix ?

One easy option is to do this for rows and columns separately A = [1 2 3 ; 4 5 6 ; 7 8 9] x = 3 ; % add a row/column of ones b...

plus d'un an ago | 2

| accepted

Answered

How can I assign a variable to all the columns and all the rows of a matrix?

Are you looking for the function SIZE? data = rand(20,10) ; [numsubs, numtrials] = size(data)

How can I assign a variable to all the columns and all the rows of a matrix?

Are you looking for the function SIZE? data = rand(20,10) ; [numsubs, numtrials] = size(data)

plus d'un an ago | 0

| accepted

Answered

Can this line in a code be written in a smarter way?

as others have mentioned before this makes ones head spin ... However, C(:,:,c)=(dC*C(:,:,c)+(dC*C(:,:,c)')'+C(:,:,c)*(-b)+C(:...

Can this line in a code be written in a smarter way?

as others have mentioned before this makes ones head spin ... However, C(:,:,c)=(dC*C(:,:,c)+(dC*C(:,:,c)')'+C(:,:,c)*(-b)+C(:...

plus d'un an ago | 0

Answered

Indexing sections of arrays

x = 101:110 n = 3 tf = mod(0:numel(x)-1, 2*n) < n a = zeros(size(x)) a(tf) = x(tf) index = find(tf)

Indexing sections of arrays

x = 101:110 n = 3 tf = mod(0:numel(x)-1, 2*n) < n a = zeros(size(x)) a(tf) = x(tf) index = find(tf)

plus d'un an ago | 1

| accepted

Answered

combinations with 2 columns

Take a look at ALLCOMB on the File Exchange: https://uk.mathworks.com/matlabcentral/fileexchange/10064-allcomb-varargin X = [1...

combinations with 2 columns

Take a look at ALLCOMB on the File Exchange: https://uk.mathworks.com/matlabcentral/fileexchange/10064-allcomb-varargin X = [1...

plus d'un an ago | 0

Answered

Combination of 4 Element

I make use of my function NCHOOSE, available here: https://www.mathworks.com/matlabcentral/fileexchange/20011-nchoose % bl is ...

Combination of 4 Element

I make use of my function NCHOOSE, available here: https://www.mathworks.com/matlabcentral/fileexchange/20011-nchoose % bl is ...

plus d'un an ago | 0

| accepted

Answered

How to find angle between two lines?

Use the dot product between the two vectors (v1 and v2) given by the four x,y pairs points. Given the formula: dot(v1,v2) = | ...

How to find angle between two lines?

Use the dot product between the two vectors (v1 and v2) given by the four x,y pairs points. Given the formula: dot(v1,v2) = | ...

plus d'un an ago | 0

Answered

The most efficient way to locate the first 1's in each row of a matrix

m = [0 1 1 1 ;1 0 1 0; 0 0 1 1 ; 0 0 0 1] % a simple solution [r, c] = find(m') ; clear v1 v1(r,1) = c % another solution ...

The most efficient way to locate the first 1's in each row of a matrix

m = [0 1 1 1 ;1 0 1 0; 0 0 1 1 ; 0 0 0 1] % a simple solution [r, c] = find(m') ; clear v1 v1(r,1) = c % another solution ...

plus d'un an ago | 0

Answered

Could anyone suggest me is there any way of choosing maximum and minimum values together

Here is a nice trick that also allows you to combine functions in a single call, which also can return the other outputs of thes...

Could anyone suggest me is there any way of choosing maximum and minimum values together

Here is a nice trick that also allows you to combine functions in a single call, which also can return the other outputs of thes...

plus d'un an ago | 1

Answered

How to have a multiple choice questionnaire that will ask the next question depending on the answer?

Take a look at all the dialog functions in matlab like questdlg, listdlg etc. Start however drawing a graph of all the question...

How to have a multiple choice questionnaire that will ask the next question depending on the answer?

Take a look at all the dialog functions in matlab like questdlg, listdlg etc. Start however drawing a graph of all the question...

plus d'un an ago | 0

Answered

how to find nearest date corresponding value ?

Does this return what you want? R = AOD_440(closestIndex, [1 2]) % select first (date?) and second (values?) columns

how to find nearest date corresponding value ?

Does this return what you want? R = AOD_440(closestIndex, [1 2]) % select first (date?) and second (values?) columns

plus d'un an ago | 0

| accepted

Answered

Plotting an Archimedean Spiral

In the computation of x and y you wrongly multiply b with Th. You should multipy by Th / (2*pi): r = 12.5; %outer radius a =...

Plotting an Archimedean Spiral

In the computation of x and y you wrongly multiply b with Th. You should multipy by Th / (2*pi): r = 12.5; %outer radius a =...

plus d'un an ago | 0

| accepted

Answered

replace duplicate value by 0 in matrix or vector

% for small vectors: b = [1 2 1 3 2 1 4 2] b(sum(triu(b == b')) > 1) = 0

replace duplicate value by 0 in matrix or vector

% for small vectors: b = [1 2 1 3 2 1 4 2] b(sum(triu(b == b')) > 1) = 0

plus d'un an ago | 1

Answered

concatenate arrays after performing addition

A little simpler than all those permutes and reshapes: A = [1 2 3 ; 4 5 6 ; 7 8 9] ; B = [2 3 4 5] ; C = repmat(A, numel(B), ...

concatenate arrays after performing addition

A little simpler than all those permutes and reshapes: A = [1 2 3 ; 4 5 6 ; 7 8 9] ; B = [2 3 4 5] ; C = repmat(A, numel(B), ...

plus d'un an ago | 0

Answered

Alternation without for loop

% a sorting trick A = [1 2 3 4 5 6 7 8 9 10] B = [0.5 0.2 0.4 0.8 0.9] C = [A B] ; [~,ix] = sort([1:numel(A) 1:numel(B)]) C...

Alternation without for loop

% a sorting trick A = [1 2 3 4 5 6 7 8 9 10] B = [0.5 0.2 0.4 0.8 0.9] C = [A B] ; [~,ix] = sort([1:numel(A) 1:numel(B)]) C...

plus d'un an ago | 0

Answered

Matching closest values to each other

For larger vectors, where BSXFUN will require a lot of memory, function NEARESTPOINT might be useful A = rand(1000000,1) ; B = ...

Matching closest values to each other

For larger vectors, where BSXFUN will require a lot of memory, function NEARESTPOINT might be useful A = rand(1000000,1) ; B = ...

plus d'un an ago | 1

| accepted

Answered

Sort Descend Scientific Notation Error

You misuse the sort command. Simply: sortedu = sort(u ,'descend') would do :-) Moreover, if you remove the semicolons, you ...

Sort Descend Scientific Notation Error

You misuse the sort command. Simply: sortedu = sort(u ,'descend') would do :-) Moreover, if you remove the semicolons, you ...

plus d'un an ago | 0

Answered

making an array to simulate states of a paramagnet with values of -1,+1.

Create a random vector with two values and map those to -1 and 1. An easy solution (with n=10): x = 2 * randi([0 1], 1, 10) - 1...

making an array to simulate states of a paramagnet with values of -1,+1.

Create a random vector with two values and map those to -1 and 1. An easy solution (with n=10): x = 2 * randi([0 1], 1, 10) - 1...

plus d'un an ago | 0

| accepted

Answered

for loop that changes specific letters to numbers

Another option: str = 'apple'; TF1 = any(lower(str) ~= 'aeiou'.')

for loop that changes specific letters to numbers

Another option: str = 'apple'; TF1 = any(lower(str) ~= 'aeiou'.')

plus d'un an ago | 2

Answered

Finding Min Value in array with changing condition

tf = a == 1 % create a logical array temp = cost(tf) % logical indexing to retrieve certain values mincost = min(t...

Finding Min Value in array with changing condition

tf = a == 1 % create a logical array temp = cost(tf) % logical indexing to retrieve certain values mincost = min(t...

plus d'un an ago | 0

Answered

How to genetate random number under constraint

Brute force attempt: N = 20 ; xyRange = [100 1900] ; minimumDistance = 200 ; attempt_counter = 1 ; Distances = 0 ; while ...

How to genetate random number under constraint

Brute force attempt: N = 20 ; xyRange = [100 1900] ; minimumDistance = 200 ; attempt_counter = 1 ; Distances = 0 ; while ...

plus d'un an ago | 0

Answered

Add an element to a 3D array

To concatenate two arrays A and B in the third dimension, use cat cat(3, A, B) Note that all the other dimensions of A and B s...

Add an element to a 3D array

To concatenate two arrays A and B in the third dimension, use cat cat(3, A, B) Note that all the other dimensions of A and B s...

plus d'un an ago | 0

Answered

To concatenate rows in matrix within for loop based on if else statement

This code is weird. In each iteration of "index" you fill either a matrix temp or a matrix temp_noise with m rows of values. T...

To concatenate rows in matrix within for loop based on if else statement

This code is weird. In each iteration of "index" you fill either a matrix temp or a matrix temp_noise with m rows of values. T...

plus d'un an ago | 1

Answered

To generate matrix from an array

More general, using indexing: A = [1 22 3 44 55 666 7 888 9] n = 2 B = A(((1:numel(A)-n).' + (0:n)))

To generate matrix from an array

More general, using indexing: A = [1 22 3 44 55 666 7 888 9] n = 2 B = A(((1:numel(A)-n).' + (0:n)))

plus d'un an ago | 0