MATLAB: Searching for vector values in a matrix indeciesMATLABmatrixvector Hi, I have a vector with 3 specific values and I want to find their indices in a matrix. Best Answer % Creating a sample matrixyourMatrix=reshape(1:15,5,3)yourMatrix = 1 6 11 2 7 12 3 8 13 4 9 14 5 10 15% Creating a sample vector with three values.yourVector=[1,7,14];% Now getting the row and cols of those values.[rows, cols]=ind2sub( size(yourMatrix), ... arrayfun(@(v) find(yourMatrix==v), ... yourVector(:)) )rows = 1 2 4cols = 1 2 3The rows and cols of 1,7,14 (the three specific values) are provided. If you are looking only for linear index number then just run:linIndex=arrayfun(@(v) find(yourMatrix==v), yourVector(:)) Related SolutionsMATLAB: Sort a specific part of a row Your question could have two meanings:1)NewVector = [YourVector(1:4), sort(YourVector(5:end), 'descend')];2)idx = find(YourVector == 4, 1, 'first');if ~isempty(idx) NewVector = [YourVector(1:idx), sort(YourVector(idx+1:end), 'descend')];else NewVector = YourVector;end MATLAB: How to find index and matched value in vector >> Vector(row,col)ans = -0.33150Or if you do not need the index then you can use logical indexing:>> Vector(abs(Vector)>=0.0001)ans = -0.33150 Related QuestionHow to populate a matrix based on a functionSelecting elements in the matrixThe minimum value of a matrix array and its indexHow to select random number in a matrix
Best Answer