matrix-indexing

3d matrix: how to use (row, column) pairs with 3rd dimension wildcard in MATLAB?

吃可爱长大的小学妹 提交于 2019-11-29 15:56:57
I have a 3 dimensional matrix, and a list of (row, column) pairs. I would like to extract the 2 dimensional matrix that corresponds to the elements in those positions, projected through the depth of the matrix. For instance, suppose, >> a = rand(4, 3, 2) a(:,:,1) = 0.5234 0.7057 0.0282 0.6173 0.2980 0.9041 0.7337 0.9380 0.9639 0.0591 0.8765 0.1693 a(:,:,2) = 0.8803 0.2094 0.5841 0.7151 0.9174 0.6203 0.7914 0.7674 0.6194 0.2009 0.2542 0.3600 >> rows = [1 4 2 1]; >> cols = [1 2 1 3]; What I'd like to get is, 0.5234 0.8765 0.6173 0.0282 0.8803 0.2542 0.7151 0.5841 maybe with some permutation of

Using a colon for indexing in matrices of unknown dimensions

血红的双手。 提交于 2019-11-29 11:01:18
When indexing matrices in MATLAB, can I specify only the first or last n dimensions, and have all others dimensions "selected automatically"? For example, I am writing a function which takes in an image, and displays it with imshow , which can either display a 3-D color image ( e.g 1024×768×3 ) or a 2-D monochrome array ( e.g 1024x768). My function does not care about how many color channels the image has, imshow will take care of that. All I want to do is pass parameters to select a single region: imshow(frame(x1:x2, y1:y2, :)) What do I put in place of the last colon to say " include all the

Can someone explain this example of deleting elements from a matrix in MATLAB?

本秂侑毒 提交于 2019-11-29 08:48:24
The following example appears in the MATLAB tutorial: X = [16 2 13; 5 11 8; 9 7 12; 4 14 1] Using a single subscript deletes a single element, or sequence of elements, and reshapes the remaining elements into a row vector. So: X(2:2:10) = [] results in: X = [16 9 2 7 13 12 1] Mysteriously, the entire 2nd row and the first two elements in the 4th row have been deleted, but I can't see the correspondence between the position of the deleted elements and the index vector 2:2:10 . Can someone please explain? The example you gave shows linear indexing . When you have a multidimensional array and you

How can I efficiently remove zeroes from a (non-sparse) matrix?

我只是一个虾纸丫 提交于 2019-11-29 01:54:05
I have a matrix: x = [0 0 0 1 1 0 5 0 7 0]; I need to remove all of the zeroes, like so: x = [1 1 5 7]; The matrices I am using are large (1x15000) and I need to do this multiple times (5000+), so efficiency is key! gnovice One way: x(x == 0) = []; A note on timing: As mentioned by woodchips , this method seems slow compared to the one used by KitsuneYMG . This has also been noted by Loren in one of her MathWorks blog posts . Since you mentioned having to do this thousands of times, you may notice a difference, in which case I would try x = x(x~=0); first. WARNING: Beware if you are using non

What does [:, :] mean on NumPy arrays

被刻印的时光 ゝ 提交于 2019-11-28 18:15:26
Sorry for the stupid question. I'm programming on PHP but found some nice code on Python and want to "recreate" it on PHP. But I'm quite frustrated about the line self.h = -0.1 self.activity = numpy.zeros((512, 512)) + self.h self.activity[:, :] = self.h But I don't understand what does [:, :] mean. Besides I wasn't able to "Google It". Full code import math import numpy import pygame from scipy.misc import imsave from scipy.ndimage.filters import gaussian_filter class AmariModel(object): def __init__(self, size): self.h = -0.1 self.k = 0.05 self.K = 0.125 self.m = 0.025 self.M = 0.065 self

Numpy advanced selection not working

旧时模样 提交于 2019-11-28 12:54:14
Can someone please help me to understand why sometimes the advanced selection doesn't work and what I can do to get it to work (2nd case)? >>> import numpy as np >>> b = np.random.rand(5, 14, 3, 2) # advanced selection works as expected >>> b[[0,1],[0,1]] array([[[ 0.7575555 , 0.18989068], [ 0.06816789, 0.95760398], [ 0.88358107, 0.19558106]], [[ 0.62122898, 0.95066355], [ 0.62947885, 0.00297711], [ 0.70292323, 0.2109297 ]]]) # doesn't work - why? >>> b[[0,1],[0,1,2]] Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: shape mismatch: objects cannot be broadcast

Matlab index to logic indexing

折月煮酒 提交于 2019-11-28 11:12:46
I have given a list of indices, e.g. i = [3 5] and a vector v = 1:6 . I need a function f which returns the logical map for the vector v given the indices i , e.g.: f(i, length(v)) = [0 0 1 0 1 0] Since I will call this function several million times, I would like to make it as fast as possible. Is there a builtin function which performs this task? I know I'm late in the game, but I really wanted to find a faster solution which is just as elegant as ismember . And indeed there is one, that employs the undocumented ismembc function: ismembc(v, i) Benchmark N = 7; i = [3 5]; %// slayton's

How do I remove elements at a set of indices in a vector in MATLAB?

久未见 提交于 2019-11-28 10:42:35
I have a vector with 100 elements. I have another vector with index positions of elements I want to remove from this vector. How do I do this? vector(indecies) = [] example: >> a = 1:10; >> a([3,4,7]) = [] a = 1 2 5 6 8 9 10 来源: https://stackoverflow.com/questions/3789847/how-do-i-remove-elements-at-a-set-of-indices-in-a-vector-in-matlab

3d matrix: how to use (row, column) pairs with 3rd dimension wildcard in MATLAB?

老子叫甜甜 提交于 2019-11-28 09:40:09
问题 I have a 3 dimensional matrix, and a list of (row, column) pairs. I would like to extract the 2 dimensional matrix that corresponds to the elements in those positions, projected through the depth of the matrix. For instance, suppose, >> a = rand(4, 3, 2) a(:,:,1) = 0.5234 0.7057 0.0282 0.6173 0.2980 0.9041 0.7337 0.9380 0.9639 0.0591 0.8765 0.1693 a(:,:,2) = 0.8803 0.2094 0.5841 0.7151 0.9174 0.6203 0.7914 0.7674 0.6194 0.2009 0.2542 0.3600 >> rows = [1 4 2 1]; >> cols = [1 2 1 3]; What I'd

Using a colon for indexing in matrices of unknown dimensions

烈酒焚心 提交于 2019-11-28 04:16:02
问题 When indexing matrices in MATLAB, can I specify only the first or last n dimensions, and have all others dimensions "selected automatically"? For example, I am writing a function which takes in an image, and displays it with imshow , which can either display a 3-D color image ( e.g 1024×768×3 ) or a 2-D monochrome array ( e.g 1024x768). My function does not care about how many color channels the image has, imshow will take care of that. All I want to do is pass parameters to select a single