10

How do I find the largest elements in an array and make the other elements zero...

 3 years ago
source link: https://www.codesd.com/item/how-do-i-find-the-largest-elements-in-an-array-and-make-the-other-elements-zero-in-matlab.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How do I find the largest elements in an array and make the other elements zero in Matlab?

advertisements

This question already has an answer here:

  • Get the indices of the n largest elements in a matrix 3 answers

Suppose I have a matrix

A=[2 3 4; 6 1 2]

I want to find 2 largest elements and make all the other elements zero. In this case A finally becomes

A=[0 0 4; 6 0 0]


Your line of action should be:

  1. Sort the matrix in descending order and obtain the order of the indices of the sorted elements.
  2. Discard the first two indices and use the rest to zero out the corresponding elements in A.

Example

A = [2 3 4; 6 1 2];
[Y, idx] = sort(A(:), 'descend')
A(idx(3:end)) = 0

This should result in:

A =
     0     0     4
     6     0     0

Related Articles

How do I find the pixel value at the bottom of an element (div)

how can i find the pixel value of the bottom of an element. eg. the #posts div. i want to create a infinite scroller. and this is so that i can detect if the user has reached near/at the bottom of the #posts and i want to load new posts if so. update

How do I find the index of the first & ldquo; Element & rdquo; In my chain using Java?

I'm working on writing a simple Prolog interpreter in Java. How can I find the last character index of the first element either the head element or the tail element of a string in "List Syntax"? List Syntax looks like: (X) (p a b) (func (func2 a

How do I find the parent element that loads the child elements without explicitly reporting it?

How can I find the parent element that loads the children elements without explicitly declaring it from another function? the jquery, $(document).ready(function(){ $('.load-child').load_child(); }); (function($) { $.fn.extend({ load_child: function(o

How can I find the column index of an element in a two-dimensional array in Lisp?

I have a 2D array, but can I find the column index of a certain element stored in an array? Suppose the element I was looking for is stored in row 2, but I don't know its column index. But how do I find the column index? For example, I have a 2D arra

How can I find the sum of the elements in a ListBox in WPF

How can I find the sum of the elements within a ListBox. I just need to find a way to store the sum of the values of the ListBox and output once the users inserts the wrong input private void theMethod(object sender, RoutedEventArgs e) { // YesButton

Table 2D: How do you find the largest sum of columns and do you return this column index?

public int largestColumnSum(){ int sum = 0; for (int i=0;i<array[0].length;i++){ for(int j=0; j<array.length; j++){ sum += array[j][i]; I got to this part but I have no idea what to do after. How should I find the largest column sum and return the i

How can I find the index of the first non-zero element in a table?

I'm using Ruby 2.4. If I want to find the first non-nil element of an array I can use a.find { |x| !x.nil? } but how do I find the index of where that first non-nil element occurred?Use either the index or find_index methods. a.index { |x| !x.nil? }

How can I find the order of an input element clicked among entries of the same type?

Let's say a form has 5 'text' inputs and 3 'radio' inputs. If the user clicks on one of the 'radio' inputs, how can I find the index of that input among inputs of the same type? In other words, if the user clicked on the second of the three radio inp

Considering a whole number, how can I find the next greatest power of two using a bit-twiddling?

If I have a integer number n, how can I find the next number k > n such that k = 2^i, with some i element of N by bitwise shifting or logic. Example: If I have n = 123, how can I find k = 128, which is a power of two, and not 124 which is only divisi

How do I find the new focus item with jquery?

I have a pop up dialog that lets you write text and does stuff when you click a button. My code is below This function works, i find the new object by looking at e.originalEvent.explicitOriginalTarget. However now i notice if i press tab this functio

How can I find the reference to the MAX () spreadsheet in a range?

With a column range of A1:A20 I have found the largest number with =MAX(A1:A20). How can I find the reference (e.g. A5) of the MAX() result? I'm specifically using Google Spreadsheets but hope this is simple enough to be standard across excel and Goo

How can I find the depth of a branch in Haskell?

I want to implement a function that gives me the depth of every branch from a tree and pack the integers of the depth in a list. I know how I can find the maximum and the minimum, but I don't know how to find the other ones. My code with an example t

How do I find the sublist indices in my list with the same items?

I have a list of different sublists: lst = [ ['Alex', 'Peeters', 22], ['Jim', 'West', 22], ['Alex', 'Wesley', 25], ['Jim', 'West', 24] ] How can I find the indices of the sublists in my list with the same first name and surname so I can delete the on

MATLAB - How do I find the first index where the value is greater than the threshold

Possible Duplicate: Given a vector a=[1,2, 3.2, 4, 5] and an element x=3 In vector a, how to find the exact entry which is bigger than x? Let's say a[] is a sorted vector. How do I find the first(smallest) index ix such that a(ix) > threshold? ix = f

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK