How to join the same matrix several times to make a big matrix in Matlab? - matlab

I have a 4x1 matrix,
A= [1;2;3;4]
I want to make B such that its size is 4x50. All the elements in the columns must contain the same elements of A. For example,
B= [1 1 1 1.... 1 1; 2 2 2 2.... 2 2; 3 3 3 3.... 3 3; 4 4 4 4.... 4 4]
In this case all the elements of A from column 1 is present in the same way in the first column of B, same for second column on B, and so on
Is there any way to form B like this from A? I was trying concatenating like below:
B= horzcat(A,A,...);
But in this case, I have to write A, 50 times. So is there any other way to get the same result?

Have you tried using repmat?
B = repmat(A, 1, 50);
repmat (which nicely stands for repeat matrix) takes a matrix and repeats itself for as many times horizontally and vertically that you want. Technically speaking, you can choose how many times you want to repeat for as many dimensions as possible as there are in your matrix. However, for our purposes here, this is a matrix which has two degrees of freedom / dimensions, so we're only considering horizontal and vertical here.
In your specific case, you want to repeat this column vector 50 times horizontally, hence the third parameter being set to 50, while you only want one copy vertically, hence the second parameter being set to 1.

Related

How does numel work in adding zeroes to change the dimension of a vector in matlab?

I am trying to add two vectors; however, they do not have the same dimensions. For example only (since the one I am doing have 1000+ values), the vectors are:
a = [1 2 3 4 5];
b = [1 2];
Since they do not have the same dimensions, I want to simply add zeroes to vector b to match the dimension of vector a.
Using the code b(numel(a)) = 0; I was able to do it. However, I am quite confused on how it worked as I only saw this code on the internet. I know that numel(a) is equal to 5, but I don't know how that code was able to add zeroes after 1 2 in variable b to match the dimension of variable a.
Can anyone explain?
When you set b(5) = 0, matlab can not simply leave the intervening elements b(3:4) unfilled, so they get zeros. If you did b(numel(a)) = 1, the intervening elements would still be filled with zero.
Keep in mind that this is a short cut that only works if you know for that fact that b is shorter than a. If not, you will be setting an element of b to 0, which is likely not what you want.

Matlab - filter matrix by having specific values

I have a matrix A and a vector b. I don't know their sizes, the size varies because it is the output of another function. What I want to do is filter A by a column (let's say jth column) which has at least one value that is in b.
How do I do this without measuring the size of b and concatenating every filtered result. Right now, the code is like this (assume j is a given value)
bsize=size(b,1);
for i=1:bsize
if i==1
a=A(A(:,j)==b(i),:);
else
a=[a; A(A(:,j)==b(i),:)];
end
end
I want to code a faster solution.
I am adding a numerical example just to make it clear. Let's say
A=[2 4
7 14
11 13
15 14]
and b=[4 14]
What I'm trying to do is filter to obtain the A matrix whose values are 4 and 14 in the second column, the elements of b to obtain the following output.
A=[2 4
7 14
15 14]
In my data A has more than 12000 rows and b has more than 100 elements. It doesn't always have to be the second column, sometimes the column index changes but that's not the problem now.
Use the ismember function to create a logical index based on column j=2 of A and vector b, and use that index into the rows of A:
output = A(ismember(A(:,j), b), :);

Finding the rows of a matrix with specified elements

I want to find the rows of a matrix which contain specified element of another matrix.
For example, a=[1 2 3 4 5 6 7] and b=[1 2 0 4;0 9 10 11;3 1 2 12]. Now, I want to find the rows of b which contain at least three element of a. For this purpose, I used bsxfun command as following:
c=find(sum(any(bsxfun(#eq, b, reshape(a,1,1,[])), 2), 3)>=3);
It works good for low dimension matrices but when I want to use this for high dimension matrices, for example, when the number of rows of b is 192799, MATLAB gives following error:
Requested 192799x4x48854 (35.1GB) array exceeds maximum array size preference.
Creation of arrays greater than this limit may take a long time and cause MATLAB
to become unresponsive. See array size limit or preference panel for more information.
Is there any other command which does this task without producing the behaviour like above for high dimension matrices?
a possible solution:
a=[1 2 3 4 5 6 7]
b=[1 2 0 4;0 9 10 11;3 1 2 12]
i=ismember(b,a)
idx = sum(i,2)
idx = find(idx>=3)

How to keep only one of consecutive repeating elements in a row matrix and represent the result by figure?

Given the matrix
A=[1 3 3 2 2 4 4 1]
I want this output: [1 3 2 4 1]
Further more, I want to make a figure like this:
Arrow heads points from 1st columnn of A to 2nd column of A then from 3rd column to 4th column and so on.
Regarding the first question:
A(diff(A)~=0)
Regarding your second question, you can use digraph.

How to extend the rows of a matrix in MATLAB filling the added rows with the first row's values efficiently [duplicate]

This question already has answers here:
Building a matrix by merging the same row vector multiple times
(2 answers)
Closed 8 years ago.
I have a matrix myVel that is of size [1 501] meaning 1 row and 501 columns.
I want to extend this matrix so that the matrix will be of size [N 501], where N is an arbitrary number.
Each of the values in the columns need to be the same (meaning that all the values in the first column are all say, x and all of the values in the second column are say, y and so on).
This means that each row would consist of the same values.
How can I achieve this efficiently?
Divakar's solution is one way to do it, and the link he referenced shows some great ways to duplicate an array. That post, however, is asking to do it without the built-in function repmat, which is the easiest solution. Because there is no such restriction for you here, I will recommend this approach. Basically, you can use repmat to do this for you. You would keep the amount of columns the same, and you would duplicate for as many rows as you want. In other words:
myVelDup = repmat(myVel, N, 1);
Example:
myVel = [1 2 3 4 5 6];
N = 4;
myVelDup = repmat(myVel, N, 1);
Output:
>> myVel
myVel =
1 2 3 4 5 6
>> myVelDup
myVelDup =
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
In general, repmat is called in the following way:
out = repmat(in, M, N);
in would be a matrix or vector of values you want duplicated, and you would want to duplicate this M times horizontally (rows) and N times vertically (columns). As such, for your case, as you have an array, you will want to duplicate this N times vertically and so we set the first parameter to N. The second parameter, the columns stay the same so we specify this to be 1 as we don't want to have any duplications... and thus the call to repmat you see above.
For more information on repmat, check out this link: http://www.mathworks.com/help/matlab/ref/repmat.html