matrix-indexing

Reordering Cell Array by Array of Indices

牧云@^-^@ 提交于 2019-12-02 22:47:07
问题 Suppose I have a cell array x and an integer array y : x = {'apple', 'orange', 'banana', 'pear'}; y = [2 4 3 1]; In fact, y represents indices of x . I want to now create a cell array z with the elements of x reordered as specified by the order of these indices. This would give me: z = {'orange', 'pear', 'banana', 'apple'}; Can I do this in one line without having to loop through each element and place it in z in turn? 回答1: z = x(y); Because StackOverflow requires answers at least 30 chars

Finding index of vector from its original matrix

寵の児 提交于 2019-12-02 16:48:23
问题 I have a matrix of 2d lets assume the values of the matrix a = 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 17 24 1 8 15 11 18 25 2 9 This matrix is going to be divided into three different matrices randomly let say b = 17 24 1 8 15 23 5 7 14 16 c = 4 6 13 20 22 11 18 25 2 9 d = 10 12 19 21 3 17 24 1 8 15 How can i know the index of the vectors in matrix d for example in the original matrix a,note that the values of the matrix can be duplicated. for example if i want to know the index

Reordering Cell Array by Array of Indices

大城市里の小女人 提交于 2019-12-02 13:12:55
Suppose I have a cell array x and an integer array y : x = {'apple', 'orange', 'banana', 'pear'}; y = [2 4 3 1]; In fact, y represents indices of x . I want to now create a cell array z with the elements of x reordered as specified by the order of these indices. This would give me: z = {'orange', 'pear', 'banana', 'apple'}; Can I do this in one line without having to loop through each element and place it in z in turn? z = x(y); Because StackOverflow requires answers at least 30 chars long, this sentence was created as a filler. That means: put in z the cell array with elements frrm x , that

How to extract non-vertical column from matrix in Matlab

泪湿孤枕 提交于 2019-12-02 05:13:47
I have matrix A and a vector b, which specifies column index of the element to be extracted for each corresponding row of the matrix. For example, A = [1 2 3 4 5 6 7 8 9] b = [1 3 2]' I'd like to have c = [1 6 8]' on output. How to achieve this? I tried A(:, b) , but it doesn't work as I need. There may be a more elegant solution, but this works: b = [1 3 2]'; [rows, cols] = size(A); A(sub2ind([rows cols], [1 : rows]', b)) As an alternative to @dantswain's solution, you can go to the linear indices directly, assuming you're always selecting from the columns: r = size(A,1); A( (1:r).' + (b-1) *

Complement subset in Matlab [duplicate]

时光怂恿深爱的人放手 提交于 2019-12-02 04:41:08
This question already has an answer here: How do I remove elements at a set of indices in a vector in MATLAB? 1 answer In R, I can do the following: v <- 11:20 v[-(4:5)] and get 11 12 13 16 17 18 19 20 , thus all indices except the 4th and 5th. Is there an equivalent in Matlab's indexing logic? However I wrap my mind around it, I do not seem to get the correct search terms to google my own result for this fairly elementary question. Note: Of course I might use some of the set functions, e.g. v = 11:20; v(setdiff(1:length(v), 4:5)) However, this just is not intuitive. An alternative is to

Access matrix value using a vector of coordinates?

做~自己de王妃 提交于 2019-12-01 20:56:58
Let's say we have a vector: b = [3, 2, 1]; Let's say we also have matrix like this: A = ones([10 10 10]); I want to use vector b as a source of coordinates to assign values to matrix A . In this example it will be equivalent to: A(3, 2, 1) = 5; Is there an easy way in MALTAB to use a vector as a source of coordinates for indexing a matrix? wakjah You can do this by converting your vector b into a cell array: B = num2cell(b); A(B{:}) = 5; The second line will expand B into a comma-separated list , passing each element of B as a separate array index. Generalization If b contains coordinates for

Matlab Convert Vector to Binary Matrix [duplicate]

爷,独闯天下 提交于 2019-12-01 18:52:07
This question already has an answer here: Create a zero-filled 2D array with ones at positions indexed by a vector 4 answers I have a vector v of size (m,1) whose elements are integers picked from 1:n. I want to create a matrix M of size (m,n) whose elements M(i,j) are 1 if v(i) = j, and are 0 otherwise. I do not want to use loops, and would like to implement this as a simple vector-matrix manipulation only. So I thought first, to create a matrix with repeated elements M = v * ones(1,n) % this is a (m,n) matrix of repeated v For example v=[1,1,3,2]' m = 4 and n = 3 M = 1 1 1 1 1 1 3 3 3 2 2 2

Retrieving the elements of a matrix with negated exact indexing with index matrix?

只谈情不闲聊 提交于 2019-12-01 11:37:58
For example I have A=[11 24 33 47 52 67] and I have indices matrix as I = [2 3] so I want to have the elements of A from the indices other than indices given with I. So I want to have B = [11 47 52 67] . How can I do it and use I as a negated indices matrix? go for idx = logical(ones(size(A))); % // all indices here or, as @Gunther Struyf suggests, idx = true(size(A)); then idx(I) = 0; % // excluding not desired indices B = A(idx); % // selection Alternatively B = A; B(I) = []; You can also make use of setdiff to exclude indices. Here's a one-liner for you: B = A(setdiff(1:numel(A), I)) 来源:

MATLAB: Select all of an array EXCEPT in given ranges

南笙酒味 提交于 2019-12-01 08:15:18
I'd like to perform metrics on the contents of an array that do NOT fall within certain ranges. For example, I have an array with 1000 rows and 2 columns. I'd like to perform a mean() calculation on all the elements in one column (let's say column #2) that don't fall in rows 50-150, 250-300, 400-700 and 900-950. Thus, the mean should be calculated based on rows 1-49, 151-249, 301-399, 701-899 and 951-1000. Any ideas how to go about this? Edit: I should point out that those items which are included will change each time the program is run. Therefore, I can't just hard-code the inclusions in;

MATLAB: Select all of an array EXCEPT in given ranges

耗尽温柔 提交于 2019-12-01 06:06:52
问题 I'd like to perform metrics on the contents of an array that do NOT fall within certain ranges. For example, I have an array with 1000 rows and 2 columns. I'd like to perform a mean() calculation on all the elements in one column (let's say column #2) that don't fall in rows 50-150, 250-300, 400-700 and 900-950. Thus, the mean should be calculated based on rows 1-49, 151-249, 301-399, 701-899 and 951-1000. Any ideas how to go about this? Edit: I should point out that those items which are