How to Select a column based on string - matlab

So I have a Cell say its 5x5
1 2 3 4 5
1 A B C D E
2 X X X X X
3 X X X X X
4 X X X X X
5 X X X X X
In Matlab how do I select a column based on the string in the first row. Say I want all the values in the C column but I dont have the index of C.

mycell(:,strcmp(mycell(1,:),'C'))

Related

Calculate minimum and maximum values of each variable in a table in kdb

Consider the following table:
sym A B
X 1 2
Y 4 1
X 6 9
Z 6 3
Z 3 7
Y 1 8
I want to find the minimum A value and maximum B value for each of my symbols X, Y & Z and display them in a new table, i.e.
sym minA maxB
X 1 9
Y 1 8
Z 3 7
Thanks.
This should do it;
select minA:min A, maxB:max B by sym from table

MATLAB sort matrix by one row

I have matrix lets say MAT of size 2*n
I want to sort the matrix by row num 2
BUT to keep each info from row 1 to its row 2 info
prev
C K A L E Y B
4 2 1 3 6 7 7
and after sort
A K L C E Y B
1 2 3 4 6 7 7
any idea?
you can use the second output argument of sort:
[~, si] = sort(MAT(2,:));
res = MAT(:,si);

Vector of indexes to create matrix by accessing elements of another matrix

Consider an n-by-k matrix M and an p-by-1 vector of indexes V ranging from 1 to n. How can I create the p-by-k matrix C where each row corresponds to the entry of M referred to by the value in each row of V.
Example
M = 1 1
1 2
1 3
1 4
and
V = 2
1
3
What I require is the matrix
C = 1 2
1 1
1 3
To assign the rows V of matrix M to a matrix C, you would write:
C = M(V,:);

Use Matlab to compare values in different columns of 2 cell arrays

I'm trying to compare two cell arrays that contain both characters and numbers. I would like to compare specific columns and then return the values in another related column.
For example, I have two cell arrays of the forms:
One= Two=
[A X 2 10 [A 1 X 2 76
B Y 2 11 B 1 Y 2 78
A X 5 22 C 1 Z 2 80
B Y 5 23 D 1 X 4 98
A X 6 28 E 1 Y 4 99
B Y 6 28 F 1 Z 4 100
C Z 6 28] G 1 X 6 110
H 1 Y 6 120]
And I want to be able to find everywhere column 2 and 3 of 'One' equals column 3 and 4 of 'Two' and return the specific value in column 5 of 'Two' (and ideally also the value in column 3). In this example, 'One' has an X 2, Y 2, X 6, and Y 6 in common with 'Two' so I would obtain a result that is:
Three=
[X 76
Y 78
X 110
Y 120]
The cell arrays I currently have are also of different sizes. Any help would be appreciated.
You can declare One and Two as cell array
Also Create one empty cell named Third. You can compare and get output as
[m n]=size(One)
Three = {};
for i=1:m
if ((One{i,2} == Two{i,3}) && (One{i,3} == Two{i,4}) )
Three=[Three; {One{i,2},Two{i,5}}];
end
end

Quick way to replicating old matrix to new one

e.g I have the original matrix (m) looks like this one
1 2
3 4
Then I use n = padarray(m,[oldMatrixRow,OldMatrixColumn]); I will have
x x x x x x
x x x x x x
x x 1 2 x x
x x 3 4 x x
x x x x x x
x x x x x x
The point here is that I would want my new matrix look like this
1 2 1 2 1 2
3 4 3 4 3 4
1 2 1 2 1 2
3 4 3 4 3 4
1 2 1 2 1 2
3 4 3 4 3 4
Is there any smart way to do that ?
Thank you very much
You want repmat
B = repmat(A,m,n)
where A is the matrix you want to repeat, and m and n define the dimensions of how it is repeated
In your case here, call your original matrix A and use
B = repmat(A,3,3)
to get your desired output
Assuming you just want to repeat your matrix a number of times the easy way would be to use repmat:
m = [1 2;3 4];
n = repmat(m,3,3)
Just to give an alternative solution:
kron(ones(3), [1 2; 3 4])