Chaining of matrix operations with indexing in Matlab [duplicate] - matlab

This question already has answers here:
How can I index a MATLAB array returned by a function without first assigning it to a local variable?
(9 answers)
Understanding why I can't use index on the result of the size function on a matrix in matlab
(1 answer)
Closed 8 years ago.
Doesn't Matlab allow chaining of matrix operations with indexing?
For eg.:
a = [1 2; 3 4];
exp(a)(:)
throws the error
Error: ()-indexing must appear last in an index expression.
It seems like this is something I would have expected Matlab to have or is there a different way to do this?

You don't need the indexing colon. The below should work.
a = [1 2; 3 4];
exp(a);

Related

What is dot multiplication [duplicate]

This question already has answers here:
Return Unique Element with a Tolerance
(7 answers)
Closed 5 years ago.
When multiplying matricies what is the difference when there is a dot there? Can this give different results
You can just use the built-in Matlab function uniquetol to obtain the unique values up to some tolerance in the array and ask the length of the returned array.
Example
A = [1+1e-11 2 3 4 1 2 3]; % generate an array with 4 unique values except for some tolerance
length(uniquetol(A, 1e-10)) % will return 4

MATLAB: fast creation of vector of indices [duplicate]

This question already has answers here:
Run-length decoding in MATLAB
(4 answers)
Closed 6 years ago.
I have a variable distr=[0 3 1 0 2];, and I have a variable full which should contrain distr(i) times i, for all i.
In this example, i want:
full=[2 2 2 3 5 5];
because distr(2)=3, therefore 3x 2, and so on.
Of course I can do it in a for-loop:
full=zeros([1,sum(distr)]);
cc=1;
for i=1:length(distr)
curr=distr(i);
full(cc:cc+curr-1)=i*ones([1,curr]);
cc=cc+curr;
end
but that is very slow. Do you know of a fast way, using MATLAB's awesome array-oriented style? Thanks!
Not sure, but maybe this will work. I can't check it since I currently don't have MATLAB:
full_tmp = arrayfun(#(i,n) i*ones(1,n),1:length(distr),distr,'uniformoutput',false);
full = cat(2,full_tmp{:});

Is there anyway to avoid loop in matlab array accessing? [duplicate]

This question already has answers here:
How can I change the values of multiple points in a matrix?
(3 answers)
Closed 6 years ago.
I have a 2-D array. And I want to access it with rows and colums index stored in another 2-D array.
Example: Now I don't want to use loops but I want to access A(1, 2) and A(3, 4).
A = ones(10,10)
B = [1, 2 ; 3, 4]
If I do A(b(:,1), b(:,2)), this will result in all possible combination of [1,2] and [3,4].
How can it be done?
Use MATLAB's sub2ind function:
A(sub2ind(size(A),B(:,1),B(:,2)))

Matlab - sum all elements above current [duplicate]

This question already has an answer here:
matlab based program where output comprises of sum of rows of input
(1 answer)
Closed 8 years ago.
I am trying to implement a vectorised solution in matlab for adding all elements above the current element in a vector. For eg.
I have a vector a as follows
a =
1
2
3
4
I would like a vector b like
b =
1
3
6
10
I know this can be done very easily using a loop but I was wondering if there are indexing options which can allow me to do the same in matlab/ octave?
You can use the Cumulative Summation function (cumsum):
b = cumsum(a)

Find the value in a matrix with certain condition [duplicate]

This question already has answers here:
Find vector elements matching two conditions in Matlab
(3 answers)
Closed 9 years ago.
How to find the value in a matrix with certain condition. For example,
a=[-3.14,2.12,-5,3,6,7];
b=find(a>0)
this will return the indices of matrix with that ">0" condition, which is b= 2 4 5 6.
do we have any solution for find the actual value in a matrix under that condition, such as returning b= 2.12 3 6 7 ?
you can even skip the find part:
whatyouwant = a(a>0);
That's called logical indexing in Matlab...
You could do the following
a = [-3.14,2.12,-5,3,6,7];
b = find(a>0)
c = a(b)
The c would then be the selected values based on the indices in b.
Hope it helps!