Answered
Add Headers to Matrix
No loops are required, just use MATLAB's ability to work with complete matrices: n = str2double(input('Select a value for n...

8 years ago | 0

Answered
Identifying and modifying the maximum in a matrix.
This is MATLAB, so no loops are required for this task. It is easy in just one line: >> M = randi(9,10,20) % fake data M...

8 years ago | 0

Answered
multiple summation of series (3 variables)
The correct tool to use is <https://www.mathworks.com/help/symbolic/double.html |double|>.

8 years ago | 0

| accepted

Answered
How to sum elements in a matrix rowwise for specific columns only?
Where |X| is your matrix: Y = [X(:,1:6),sum(X(:,7:10),2)]

8 years ago | 0

| accepted

Answered
the last output given using for loop
_"why the following for loop produces e(end) of only n=5..."_ You define a vector |e| with five elements: e = linspace(-...

8 years ago | 1

| accepted

Answered
added different number of column using for loop
>> c = repmat(b,1,ceil(numel(a)/numel(b))); >> c = c(1:numel(a)) c = 1 2 1 2 1 2 1 2 1

8 years ago | 0

Answered
How to vectorize this for loop
*Method one:* If |X| only contains ones (like in your example) S = K*sum(max(B,[],1)) *Method two:* If |X| contains valu...

8 years ago | 0

| accepted

Answered
Save image to chosen folder
You need to get the second (path) output from <https://www.mathworks.com/help/matlab/ref/uiputfile.html |uiputfile|>: [useN...

8 years ago | 0

| accepted

Answered
Merge multiple .mat files based on specific file name structure
Something like this might get you started: for k = 1:31 fmt = sprintf('AB%d*.mat',k); S = dir(fmt); N = {...

8 years ago | 0

| accepted

Answered
How to speed up plotting from within a loop
You should use the profiler to find where the slow parts of the code are: <https://www.mathworks.com/help/matlab/matlab_prog/...

8 years ago | 1

| accepted

Answered
How can I find the maximum number and minimum number in a matrix?
Where |M| is your matrix: max(M(:)) min(M(:))

8 years ago | 1

Answered
How can I move a number in a matrix in certain directions?
Where M is your matrix, this might get you started: [R,C] = find(M); M(R,C) = 0; M(R+randi(-1:1),C+1) = 1; You wil...

8 years ago | 0

Answered
How to get query point for a particular known interpolated value?
>> interp1(v,x,25) ans = 1.5909

8 years ago | 0

| accepted

Answered
Difference between mod and rem functions
The outputs of |rem| and |mod| are the same if the inputs have the same sign, otherwise it depends on how the division is interp...

8 years ago | 3

Answered
How do I declare a multiline array?
y = [180,183,188,191,195,199,201,195,207,210,212,216,220,221,224,... 221,225,229,234,237,239,242,242,244,245,244,246,245,...

8 years ago | 7

| accepted

Answered
i have a matrix like [1 0 0 1 0 1 0 ; 0 1 0 1 0 0 ; 1 0 0 1 1 1]. there are 3 rows and 7 column i wanna to see in the format like
>> M = [1 0 0 1 0 1 ; 0 1 0 1 0 0 ; 1 0 0 1 1 1] M = 1 0 0 1 0 1 0 1 0 1 0 0 1 0 ...

8 years ago | 0

| accepted

Answered
Sort Numbers correctly in an uitable
I am sure that you could find some Java add-on that provides this functionality, but if you want to sort strings with numeric va...

8 years ago | 0

Answered
Calculation of conditional expectations
sum(A)./nnz(A>=0)

8 years ago | 0

| accepted

Answered
Why is Matlab saying that my index is not an integar when it actually is?
The best solution is to not use fractional numbers for generating indices. The easiest way to do this is to simply put the fract...

8 years ago | 0

Answered
Creating a random matrix with different probabilities
P = rand(52,1); P = (P>0.2) + (P>0.66) + (P>0.82) Note that the values used here are just |cumsum([0.2,0.46,0.16])|, i.e...

8 years ago | 2

| accepted

Answered
Heya i'm really battling with saving values that are non integer non positive. If i don't try to index them then it only gives the last value.
The main problem I can see is this: for theta=0:length(theta) When you redefine |theta| to be the loop variable, then th...

8 years ago | 0

| accepted

Answered
regex replace whole word
>> regexprep('v1 = v1a','^([a-z]+)(\d+)','$1($2)') ans = v(1) = v1a Hopefully you are not constructing arbitrary strings...

8 years ago | 0

| accepted

Answered
change < A x B x C double> to < B x C x A double >
Either <https://www.mathworks.com/help/matlab/ref/reshape.html |reshape|> or <https://www.mathworks.com/help/matlab/ref/permute....

8 years ago | 0

Answered
How to change array format - change row number and column number, but have all the data as before
Depending on the required element order, (along 2nd dim first): B = reshape(A.',3,[]) or (along 1st dim first): B =...

8 years ago | 0

| accepted

Answered
I have this code which is behaving differently for decimal and integer values. Where am I missing??
_"That is really weird indeed. Perhaps MATLAB struggles to determine equality for floating point numbers"_ Not at all: if two...

8 years ago | 1

| accepted

Answered
In switch-case, case values from variable (dynamic)
Use cell arrays, where each cell contains one scalar numeric (or a char vector): V1 = {1,2,3}; V2 = {4,5,6}; switch s...

8 years ago | 0

| accepted

Answered
Saving Data for all files to matrix
You need to reference the output variable using indexing, otherwise you simply overwrite it on each loop iteration. Assuming tha...

8 years ago | 0

| accepted

Answered
How to create a matrix with 1 on ij-th position and zeros elsewhere from a lower triangular matrix?
A = [2,4,6;4,8,11;6,11,20] S = size(A); T = tril(true(S)); [R,C] = find(T); N = nnz(T); Z = zeros([S,N]); V...

8 years ago | 0

| accepted

Answered
Check if all vector elements have same sign
|true| if all non-zero elements of |V| have the same sign: ~any(diff(sign(V(V~=0))))

8 years ago | 1

| accepted

Answered
I have a cell array of 1 x 8 size.I want to add a zero in between two cells.How do i do this in matlab?
>> V = [-4.5,-1.5 ,-2.5,-1.2,1.2,2.5,1.5,4.5]; >> Z = V; >> Z(2,:) = 0; >> Z = Z([true(size(V));~diff(abs(V)),false])...

8 years ago | 0

| accepted

Load more