Matlab - select all rows starting with a particular number - matlab

Say I have a matrix A, and I want to construct a matrix B that contains all rows of B that start with a particular number. How?
Thanks

Select all rows of B into A, where the first colum of B has value n:
A = B(B(:,1) == n,:);
In contrast to that, the following selects all rows of B into A, starting from row index n:
A = B(n:end,:);

Related

Separating some part of a matrix in matlab

I have a 2m by 2 matrix called A and a 2 by m matrix called B. Let's name the product of A by B, C:
C= A*B;
which C is a 2m by m matrix. I want to find matrix F which contains some parts of C. F is a m by 2 matrix and contains elements C(1,1), C(2,1), C(3,2),C(4,2),C(5,3), C(6,3),...C(2m-1,m), C(2m,m).
For example, consider
A = [0,2;1,3;4,7;8,3;4,5;1,2]
B=[1,4,6;5,7,3]
C=A*B;
In this case:
F=[C(1,1),C(2,1);C(3,2),C(4,2);C(5,3),C(6,3)]
But I like to find F without calculating all elements of C. Because I think calculating all elements of C would be time wasting for large values of m. Could anyone suggest a way to find F in general case?
Using Indexing:
F = [C(1:2*m+2:end);C(2:2*m+2:end)]'
To find F without calculating C you can instead use:
F=cell2mat(arrayfun(#(x) A(2*x-1:2*x,:)*B(:,x), 1:m,'uniformoutput',0))'
(You must set m, A, and B as defined in the question)
Explanation:
Each row of F is the transpose of the product of a submatrix in A and a column of B. For example, the first row in F is the transpose of:
A(1:2,:)*B(:,1)
The next row is the transpose of:
A(3:4,:)*B(:,2)
etc.
So this method only calculates the necessary values, by multiplying each column of B only by the corresponding submatrix of A and avoids calculating the unused values in C.

Reshape by Certain Name in Matlab

I have a cell-matrix like the below,
the first column: A, B, C
the second column: A, B, D
the third column: 1, 1, 1
Which means that A and A has 1 unit, B and B has 1 unit and C and D has one unit
How can I conveniently create the following matrix (mat) in matlab?
[Name,A,B,C,D
A,1,NA,NA,NA
mat = B,NA,1,NA,NA
C,NA,NA,NA,NA
D,NA,NA,NA,1]
I think I can use a loop to achieve that, but actually the dimension is much larger than the example able. How can I do that?
A, B, C, D here are characters, if the matrix cannot contain both numeric and character, I can remove the first column and the first row in mat. Also actually the first matrix containing the relationship of A, B, C, D is a 3*3 cell.
Desired output can be produced:
mycell = {...
'A' , 'B' , 'C'
'A' , 'B' , 'D'
1 , 1 , 1};
[U, ~, IDX] = unique(mycell(1:2,:));
IDX = reshape(IDX,2,[]).';
mat = accumarray(IDX,[mycell{3,:}],[],[],NA);
so U is names of rows/columns
IDX shows indices of rows and columns of mat that contain non NA values
accumarray is the function that can convert indices and values to desired matrix
Now you can access elements of mat by numeric indices i.e. mat(1,3) that represent relationship of A and C
If you want to work with character indices instead of numeric ones you can do this:
index=cell2struct(num2cell(1:numel(U)),U,2);
mat(index.A, index.C)
index is a struct that maps characters to numbers. Also you can use index this way:
mat(index.('A'), index.('C'))

filtering rows by its second values in matlab

I am new in Matlab I hope this question worth it.
I have 2 sets of numbers.
a=[0;0.01;0.02;0.03;...] (4187*1)
b=[0;0.3;0.4;0.1;0.23;...] (4187*1)
By defining f=[a b] (4187*2) Matrix I want to filter my rows by condition on b value (b>0.05) and I want to see the results in (a b) format.
Can you help me on writing this program?
If you want to filter your rows depending on values in b then you can do this:
f = [a,b];
f = f((f(:,2)<=0.05),:);
This will keep all of the values less than or equal to 0.05 based on the values from b. This is assuming you want to delete all the rows of b>0.05
If you want to keep the values of b>0.05 then you would use this:
f = [a,b];
f = f((f(:,2)>0.05),:);

Matlab: Match two arrays based on dates (unsorted)

I have two arrays of dates, A and B, where size(A) > size(B).
A contains multiple entries for each date (it corresponds to a cell containing various data).
B is simply all dates between the start and end date, but also correspond to certain data.
I want to create and array, C (where size(C) == size(A), containing the row number in B corresponding to the date on each row in A (so that data can be cross-referenced, i.e. perform a calculation based on data in A and B, using the row index of B to match dates).
I can do this using a loop and the find function:
for i=length(A)
C(i) = find(A(i) == B);
end
However, this is probably not the most efficient solution (takes quite a long time given my large data set). I'd "prefer" simply C = find(A == B), but Matlab does not allow this.
Is there a way to achieve the same result not using a loop?
Many thanks for any help!
You can use second output argument of ismember, like so:
[tf, C] = ismember(A, B);
You can use the second output of histc for this
# Put some dates in B
B = datenum('01-Jan-2013') : datenum('05-Jan-2013');
# Make A into a superset of B
A = B(randi(length(B), 1, 20));
# The vector C holds the indexes you want
[tmp, C] = histc(A,B);
The function you need is intersect.
Use it like this:
[a,b,C] = intersect(A,B)
While the results for a and b does not matter. The result for C is the C that you want

Randomly selected matrix with no repetition of rows along with its index values in matlab

I have a 7x10 matrix, from this matrix I want to randomly select 4 rows without any repetition and this selection will include the index values of the selected rows. So, my question is: how to get a randomly selected matrix with no repetition of rows along with its index values of the original matrix from where it is selected?
Is this what you are after?
B = A(randperm(size(A,1),4),:)
Update: (thanks to federico)
idx = randperm(size(A,1),4);
B = A(idx,:)
Now idx will be a set of 4 integers between 1 and n, where n is the number of rows in A.
A(idx,:) gives you the elements in the rows represented by idx.