Assign to an Array and Replace Emerged Nil Values

Assign to an array and replace emerged nil values

There is no built-in function to replace nil in an array, so yes, map is the way to go. If a shorter version would make you happier, you could do:

array.map {|e| e ? e : 0}

How can I replace given values in an array with different values?

You could use map over your array and to check if the element is nil, if it's so then replace with "0", if not then leave the value:

array = [nil, "2", nil, nil, "f"]
p array.map{|e| e.nil? ? '0' : e}
# => ["0", "2", "0", "0", "f"]

Another way is to use the Rails Object#presence method:

[nil, "2", nil, nil, "f"].map{|e| e.presence || '0'}
# => ["0", "2", "0", "0", "f"]

How do I initialize an empty array in C#?

If you are going to use a collection that you don't know the size of in advance, there are better options than arrays.

Use a List<string> instead - it will allow you to add as many items as you need and if you need to return an array, call ToArray() on the variable.

var listOfStrings = new List<string>();

// do stuff...

string[] arrayOfStrings = listOfStrings.ToArray();

If you must create an empty array you can do this:

string[] emptyStringArray = new string[0]; 

Replacing zero elements in my image array on python

I used the second answer from the link, tell me if this is close to what you want, because it appeared to be what you wanted.

Creating one sample image and center it, so it's somewhat close to your first example image.

import numpy as np
import matplotlib.pyplot as plt
image = np.zeros((100, 100))
center_noise = np.random.normal(loc=10, size=(50, 50))
image[25:75, 25:75] = center_noise
plt.imshow(image, cmap='gray')

Output

Inspired by rr_gray = np.where(rr_gray==0, np.nan, rr_gray) #convert zero elements to nan in your code, I'm replacing the zeros with NaN.

image_centered = np.where(image == 0, np.nan, image)
plt.imshow(image_centered, cmap='gray')

Output

Now I used the function in the second answer of the link, fill.

test = fill(image_centered)
plt.imshow(test, cmap='gray')

This is the result

With fill

I'm sorry I can't help you more. I wish I could, I'm just not very well versed in image processing. I looked at your code and couldn't figure out why it's not working, sorry.

How to extend an existing JavaScript array with another array, without creating a new array

The .push method can take multiple arguments. You can use the spread operator to pass all the elements of the second array as arguments to .push:

>>> a.push(...b)

If your browser does not support ECMAScript 6, you can use .apply instead:

>>> a.push.apply(a, b)

Or perhaps, if you think it's clearer:

>>> Array.prototype.push.apply(a,b)

Please note that all these solutions will fail with a stack overflow error if array b is too long (trouble starts at about 100,000 elements, depending on the browser).

If you cannot guarantee that b is short enough, you should use a standard loop-based technique described in the other answer.

Replacing string values in cell array with numbers

Do you want to use the characters '1', '2', '3' or just the numbers 1, 2, 3? The distinction is the difference between a 1 line answer and a 2 line answer!

Based on your example, let's use the following data:

arr = {'FRD'; 'FRD'; 'OTH'; 'UNFRD'; 'OTH'; 'FRD'};

Get the row index within my_des of each element in arr, and use that to get the corresponding 2nd column values...

% If you just want the *number* then this is all you need
[~, idx] = ismember(arr, my_des);
% idx is the row within column 1 of my_des where the value in arr is found
% >> idx = [1; 1; 3; 2; 3; 1]

% If you want to get the values my_des then use idx as a row index
out = mydes(idx, 2);
% out is the corresponding values from the 2nd column of my_des, whatever they may be.
% >> out = {'1'; '1'; '3'; '2'; '3'; '1'};

Aside: why are you declaring a cell array by concatenating 1-element cell arrays for my_des? Instead, you can just do this:

my_des = {'FRD',   '1'; 
'UNFRD', '2';
'OTH', '3'};

Python/NumPy: find the first index of zero, then replace all elements with zero after that for each row

One way to accomplish question 1 is to use numpy.cumprod

>>> np.cumprod(a, axis=1)
array([[1, 0, 0, 0, 0],
[1, 1, 1, 1, 0],
[1, 0, 0, 0, 0],
[1, 0, 0, 0, 0]])

Get all unique values in a JavaScript array (remove duplicates)

With JavaScript 1.6 / ECMAScript 5 you can use the native filter method of an Array in the following way to get an array with unique values:

function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}

// usage example:
var a = ['a', 1, 'a', 2, '1'];
var unique = a.filter(onlyUnique);

console.log(unique); // ['a', 1, 2, '1']

Replace Text in val with array - jquery

You need to iterate through the array and replace

$(document).ready(function () {

$('#submit').click(function () {

var array = [];

array[0] = ['#1', 'Value 1'];
array[1] = ['#2', 'Value 2'];
array[2] = ['#3', 'Value 3'];

$('#article').val(function (i, v) {
$.each(array, function (i, arr) {
v = v.replace(arr[0], arr[1]);
})
return v;
});
return false;
});
});

Demo: Fiddle

Replace duplicate numbers with unique numbers from 0-(N-1)

The fastest way to do this is probably the most straightforward one. I would take a pass through the list of data keeping a count of each distinct value and marking where duplicates appeared. Then it is just a matter of forming a list of unused values and applying them in turn at the places where duplicates were found.

Tempting as it may be to use a C++ List, if speed is of the essence a simple C array is the most efficient.

This program show the principle.

#include <iostream>
#include <cstring>

using namespace std;

int main()
{
int data[] = { 10, 4, 5, 7, 10, 9, 10, 9, 8, 10, 5 };
int N = sizeof(data) / sizeof(data[0]);

int tally[N];
memset(tally, 0, sizeof(tally));

int dup_indices[N];
int ndups = 0;

// Build a count of each value and a list of indices of duplicate data
for (int i = 0; i < N; i++) {
if (tally[data[i]]++) {
dup_indices[ndups++] = i;
}
}

// Replace each duplicate with the next value having a zero count
int t = 0;
for (int i = 0; i < ndups; i++) {
while (tally[t]) t++;
data[dup_indices[i]] = t++;
}

for (int i = 0; i < N; i++) {
cout << data[i] << " ";
}

return 0;
}

output

10 4 5 7 0 9 1 2 8 3 6


Related Topics



Leave a reply



Submit