What is the function of comma and colon together? [duplicate] - matlab

This question already has answers here:
The meaning of colon operator in MATLAB
(2 answers)
Closed 4 years ago.
In this code:
hdrMat(ctr,:) = [double(frameCtr) double(numBins) binLength Fs Fc RangeOffset];
FrameMat(:,ctr) = data;
What is the meaning of (ctr,:) and (:,ctr) in terms of vectors?

The (ctr,:) means you are addressing the ctr'th row, starting from the first row as row nr. 1. The ":" states, that you are addressing the whole row and not just an element.
The (:,ctr) means you are addressing the ctr'th column and again " : " tells matlab to address the entire column.
Example:
A = [1 2 3; 4 5 6; 7 8 9];
A(2,:) = [0 1 0]
%Output
[1 2 3]
A = [0 1 0]
[7 8 9]
You can also apply the colon operator " : " to address a certain range of the row/column by writing:
A(2:3,1)
%Output
[0; 7];
Id strongly recommend you looking into the basic matlab questions on StackOverflow and also on the MatLab official documentation, where lots of examples are given.
Cheers, Pablo

Related

Calculate the first N terms of a geometric sequence in Matlab [duplicate]

This question already has answers here:
Common way to generate finite geometric series in MATLAB
(2 answers)
Closed 7 years ago.
How to calculate the first N terms of the geometric sequence Un = 2^n in Matlab?
Are there any Matlab functions that I'm not aware of to facilitate this? or do I have to pick a math book to understand this and implement it in a for loop or something?
Any links to similar Matlab code would be appreciated, or if you could explain it for me that would be appreciated!
First, you set the N terms for your sequence, i.e.:
N = 10 %//set first 10
Now you want to make a vector from 1 to N, i.e.:
n= [1:N]
Un = 2.^n %//Note the dot is very important! I almost forgot
%//ans = [2,4,8,16...1024]
This would make function a vector of 1 by N where each element is the corresponding answer to your function.
for your second question (in comment)
you want to do something like:
Bflip = B' %//This flips the matrix B so that what use to be column is now rows
So Bflip would be the result you want, I tested with your example:
A = [2 2 2;4 4 4; 6 6 6];
B = [0 0 0; 1 1 1; 2 2 2];
Bflit = [ 0 1 2
0 1 2
0 1 2]
This will generate a 3 dimension matrix. To call on each of the 4 sets of results, just do something like result1 = permutation(:,:,1)

Every two elements mean between these two element [duplicate]

This question already has answers here:
How do I double the size of a vector in MATLAB with interpolation?
(2 answers)
Closed 7 years ago.
I am a newbie for Matlab. I try to take every two consecutive elements' means and put it between these two consecutive elements. For example;
If I have a vector like below:
a=[1 2 5 4 3 6]
At the end I need b like:
b=[1 1.5 2 3.5 5 4.5 4 3.5 3 4.5 6]
It can be done via loops but I try to do via matlab function is it possible to do ?
The "brute force way":
b = zeros( 1, 2*numel(a)-1 );
b(1:2:end) = a; % take care of the original values
b(2:2:end) = 0.5*( a(1:end-1) + a(2:end) ); % the mean
Using interp1:
b = interp1( 1:2:(2*numel(a)-1), a, 1:(2*numel(a)-1), 'linear' )

Matlab vectorization of vector creation [duplicate]

This question already has answers here:
Element-wise array replication according to a count [duplicate]
(4 answers)
Closed 8 years ago.
I would like to vectorize the creation of the following vector:
For example-
Let A be a vector [5 3 2 1]
And let B be a vector [1 2 3 4]
I would like C to be the vector [1 1 1 1 1 2 2 2 3 3 4]
Meaning- each element i in B is duplicated A(i) times in C.
I haven't found a way to vectorize the creation of this, any ideas?
Thanks in advance!
Ronen
Approach #1
Here's one approach if B doesn't have any zeros -
C = nonzeros(bsxfun(#times,bsxfun(#le,[1:max(A)]',A),B))
Approach #2
A general case solution -
mask = bsxfun(#le,[1:max(A)]',A) %//'
B_ext = bsxfun(#times,mask,B)
C = B_ext(mask)
Approach #3
cumsum based approach and must be pretty efficient one -
idx = [1 cumsum(A(1:end-1))+1] %// indices where each new B values start
C = zeros(sum(A),1) %// storage for output
C(idx) = diff([0 B]) %// put those values, but offseted
C = cumsum(C) %// finally get the output

use vector to select columns of a matlab array [duplicate]

This question already has answers here:
Get different column in each row
(2 answers)
Closed 8 years ago.
This is an issue i commonly find myself trying to solve. I have the following:
A = [1 2;
3 4;
5 6;
7 8;
9 10];
B = [1,2,1,2,2];
On each row (i) of A, i want to return the value of the column specified in B(i). I currently solve the problem using a loop:
result = zeros(size(B));
for i=1:length(B)
result(i) = A(i,B(i));
end
Where result = [1 4 5 8 10]
But this seems inelegant to me. Is there a one-liner?
You could get the correct linear indices using sub2ind:
rows = (1:numel(B))'
cols = B(:);
ind = sub2ind(size(A), rows, cols);
A(ind)
or in a one-liner
A(sub2ind(size(A), (1:numel(B))', B(:)))
or a more elegant method (taken from the 2nd answer to the duplicate question)
diag(A(:,B))
I can't tell you about performance though...

What does the tilde symbol after an equals do? [duplicate]

This question already has an answer here:
Matlab Operators
(1 answer)
Closed 9 years ago.
What does the tilde symbol ~ do in Matlab?
for example, I've got the Matlab line, in which a and b are tables.
a =~b;
Basically a is assigned to the result of the logical not operator applied to b
For example if b is the matrix
b = [12 0 10]
then
a = ~b
a = [0 1 0]
See http://www.mathworks.co.uk/help/matlab/ref/not.html for details