Interleaved repmat [duplicate] - matlab

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Element-wise array replication in Matlab
I have a m x 1 vector that I would like to repeat n times to create a (m*n)x1 vector. If I use repmat, I get something like
>> V = [a;b;c];
>> repmat(V,2,1) % n = 2, m = 3
a
b
c
a
b
c
What would be a one-line (and hopefully fast) way of getting the vector
[a;a;a;b;b;b;c;c;c]
for arbitrary n and m?

V=[ 1;2;3];
reshape(repmat(V',3,1),[],1)
ans =
1
1
1
2
2
2
3
3
3

Related

Finding value that is similar and available in another vector [duplicate]

This question already has an answer here:
Matlab, finding common values in two array
(1 answer)
Closed 5 years ago.
Let say we have 2 vectors of A and B,
A=[1;2;5;6;7;9]; B=[1;3;4;7];
How to find value C that are available in both A and B? The expected value should be
C=[1;7]
Since the title of your question says "similar", I assume you want to compare with a given tolerance. For that you can use ismembertol:
tol = 1e-3;
A = [1; 2 ; 5 ; 6 ; 7 ; 9];
B = [1.0001; 3.0001; 4.0001; 7.0001];
ind = ismembertol(A, B, tol);
C = A(ind);
Very simple:
A=[1;2;5;6;7;9];
B=[1;3;4;7];
C=intersect(A,B)

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

Magnitude of each column in a matrix [duplicate]

This question already has answers here:
Vector norm of an array of vectors in MATLAB
(4 answers)
Closed 5 years ago.
I have an input matrix that has 3 rows and 1000 columns. Each column represents and x, y, z variable. I want to find the magnitude of each column and store that in an output matrix that has 1 row and 1000 columns.
This is my current attempt but it doesn't seem to be working:
output(1,:) = norm(input(3,:));
my input matrix looks like:
x1, x2,...,x1000
y1, y2,...,y1000
z1, z2,...,z1000
I want my output matrix to look like:
[magnitude(x1,y1,z1), magnitude(x2,y2,z2),...,magnitude(x1000,y1000,z1000)]
Any help would be greatly appreciated.
norm(input(3,:)) will give you the norm of the 1000 elements of the third row.
Easy solution is to just run a for loop.
output = zeros(1,1000); %Preallocate space
for i = 1:length(output)
output(i) = norm(input(:, i));
end
MATLAB's norm function only works for single vectors. Let A be the name of the matrix which columns you want to find the norm to. Then this command does the job:
norm_A = sqrt(sum(A.*A));
Here is an example:
>> A = [1:5; 1:5; 1:5]
A =
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
>> norm_A = sqrt(sum(A.*A))
norm_A =
1.7321 3.4641 5.1962 6.9282 8.6603