In the matrix shown below how can I select elements 01, 09, 17 and 25. From Egon's answer to my earlier question Select Diagonal Elements of a Matrix in MATLAB I am able to select the central value 25 using c = (size(A)+1)/2; but I am wondering how to select above mentioned elements in NW direction.
A = [01 02 03 04 05 06 07
08 09 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35
36 37 38 39 40 41 42
43 44 45 46 47 48 49];
Use diag to get elements on the diagonal.
diagA = diag(A)
You can restrict this to the elements from the top left to the middle with
n = ceil(size(A, 1) / 2)
diagA(1:n)
Another way to do this is with linear indexing. If you have an N-by-N matrix, you can select the elements you want as follows:
values = A(1:N+1:ceil((N^2)/2));
Related
Please help me.
I looked for the way to graph the following table and I could not.
Take the column "HR" for the X axis and "VALUE" for Y axis, the problem is that the graph is cut. Column "RNO" only for sort.
For example: the value graph X Axis 10,11, ..., 21,22,23 when reaching the value or 00 does not continue the graph in the X axis, what happens is that values less than 10 puts them first. I attach the image that appears to me:
HR RATE_TIMEWAITED_PER_CLASS RNO
11 1
12 135.083333 2
13 232.916667 3
14 130.611111 4
15 155.111111 5
16 186.472222 6
17 166.805556 7
18 110.916667 8
19 89.3055556 9
20 198.166667 10
21 56.0277778 11
22 32.0277778 12
23 29.4722222 13
00 501.111111 14
01 18.6944444 15
02 16.0555556 16
03 14.1666667 17
04 375.892811 18
05 16.0833333 19
06 29.8611111 20
07 79.6666667 21
08 131.25 22
09 332.666667 23
example
My text file is like
string 1
12 13 14 16
11 41 25 26
32 25 26 27
string 2
23 15 26 28
12 15 19 17
35 65 84 12
string 3 and so on...
I want that if I ask for string 1 , it will give me the corresponging matrix under string 1 and the size of the matrix is also not known i.e.
12 13 14 16
11 41 25 26
32 25 26 27
will anyone tell me how to do this?
thanks
You should do the following:
1 - Convert string to integer/matrix
M1 = str2num(string1) - http://www.mathworks.com/help/matlab/ref/str2num.html
M2 = str2num(string2)
2 - If you want to find the corresponding values, just compare:
for i=1:length(M1)
[~,M3(i)]=ismember(M1(i),M2)
end
M3 will give you the indexes that are a match in M2.
I am working on Matlab and I have a 16x16 matrix where the column values are equal to the row values matrix of nucleotide substitutions
I would like to reshape it so I have only 1 row containing only the unique values (in other words, I would like a row with:
7816 0 ....6432 0 ....8148 20.....
I tried B = reshape(matrix,1,[]); and it works but unfortunately it is giving me also the non-unique values (it is basically taking every row and pasting it just next to the previous one).
Is there any way to do this? Thanks!
Given a symmetric input matrix A:
>> A = randi(30, 5)
A =
9 13 19 23 8
13 4 5 14 19
16 25 13 11 27
12 3 20 25 11
1 12 9 20 27
>> A = A + A.'
A =
18 26 35 35 9
26 8 30 17 31
35 30 26 31 36
35 17 31 50 31
9 31 36 31 54
>> A(A < 10) = 0
A =
18 26 35 35 0
26 0 30 17 31
35 30 26 31 36
35 17 31 50 31
0 31 36 31 54
You can extract the lower triangular portion and turn it into a vector like so:
>> B = A(find(tril(ones(size(A))))).'
B =
18 26 35 35 0 0 30 17 31 26 31 36 50 31 54
Notice that this skips the 26 in the second column, the 35, 30 in the second column, and so on.
This takes the lower triangular portion of a matrix of 1's the same size as A and finds the indices of all of the 1 values. (That gets around the 0 values in the original matrix.) Then it uses the locations of the 1's returned by find to index into the original matrix A. Transpose to make it a row vector.
I'm trying to generate a matrix in which each element is defined as 10 * row_index + column_index. The rows and columns may fluctuate up to a 9x9 matrix. For example:
11 12 13 14 15 16
21 22 23 24 25 26
31 32 33 34 35 36
41 42 43 44 45 46
51 52 53 54 55 56
The algorithm is exceedingly simple with for loops, but I've been warned that I should avoid for loops unless absolutely necessary, when dealing with matrices, because they are slower than vector/matrix operations.
What other ways are there to generate such a matrix in Matlab 2012b?
For your particular matrix, it's quite straightforward:
nRows = 4;
nCols = 5;
out = bsxfun(#plus,10*(1:nRows)',1:nCols)
out =
11 12 13 14 15
21 22 23 24 25
31 32 33 34 35
41 42 43 44 45
I'm new in Matlab and I need to connect elements from two columns in different rows. I have the following matrix:
a =
12 15
14 16
15 20
16 23
20 21
21 25
23 24
24 26
25 27
what I need is to identify the elements of the 2nd column that are equal to the elements in the first, and join them sequentially to create 2 rows : [12 15 20 21 25 27] and [14 16 23 24 26]. The actual data is quite large, so the more vectorized the code, the better!
Thanks for your help!
GIE