How do I check if an array index is out of range SWIFT [duplicate] - arrays

This question already has answers here:
Swift Array - Check if an index exists
(12 answers)
Closed 7 years ago.
I want to be able to check when my array index goes out of range. The array elements are all strings, so I tried to do something like this but it doesn't work:
if questions[3] != nil {
}
Can someone just show me how to check?

Before indexing into the array, you need to check:
The intended index is not below 0
array's count is above the intended index
let intendedIndex: Int = 3
if (intendedIndex >= 0 && questions.count > intendedIndex) {
// This line will not throw index out of range:
let question3 = questions[intendedIndex]
}

Related

Check item contains in model object array in swift 3 [duplicate]

This question already has answers here:
how can I check if a structure is in the array of structures based on its field in Swift3?
(3 answers)
Get element from array of dictionaries according to key
(1 answer)
Closed 5 years ago.
I have a model object called user.
how to find my user.id from all users collection.
I need something like this UsersCollection.contains(user.id)
and returns Bool value.
Here you go.
let contains = UsersCollection.contains{ $0.id == 0 }
Edit:
let object = UsersCollection.first{ $0.id == 0 } // object is optional here.

Removing multiple elements from array by index in Ruby [duplicate]

This question already has answers here:
Delete contents of array based on a set of indexes
(4 answers)
Closed 5 years ago.
I have an array. For example,
x = [1,2,3,4,5]
I know the command
x.delete_at(i)
will delete the element at index i from the array. But from what I've read, it can only handle one argument.
Let's say I have a variable that stores the indexes I wish to remove from x. For example,
y = [0,2,3]
My question: Is it possible to remove multiple elements from an array using another array that stores in the indexes you wish to delete at?
In essence, something like
x.delete_at(y)
Thanks! :)
You can use reject with with_index:
x.reject.with_index { |e, i| y.include? i } #=> [2, 5]

MATLAB: cannot call or index into a temporary array [duplicate]

This question already has answers here:
How can I index a MATLAB array returned by a function without first assigning it to a local variable?
(9 answers)
Closed 5 years ago.
I am trying to read data files each containing one column and 4097 rows. But my function needs total numbers of rows with even number (means 4096). So I used the MATLAB command x(2:length(x))). But my 'x' value in this command is a(:,k) and the issue is MATLAB cannot call or index into a temporary array. Any solution to this? I thank all for the support.
The code is:
for k = 1:9
with filename = sprintf('F00%d.txt',k);
a(:,k) = load(filename);
x = a(:,k)(2:length(a(:,k)));
w = tqwt(p,1,3,3);
[a1,a2,a3,a4]= deal(w{:});
m(a1,1) = mean(a1);
s(a1,1) = std(a1);
ma(a1,1) = max(a1);
mi(a1,1) = min(a1);
Unfortunately, you have to split x = a(:,k)(2:length(a(:,k))); into two lines as shown below:
temp = a(:,k);
x = temp(2:length(a(:,k)));
Please read:
Indexing of a function's return
How can I use indexing on the output of a function?

Delete all occurrences of value in Swift Array [duplicate]

This question already has answers here:
Remove matched item from array of objects?
(5 answers)
Closed 7 years ago.
So I have an array of Int's and I am trying to create a function to delete a certain value from the array. However, the removeAtIndex() function only deletes the first occurrence and the removeLast() only removes the last. I tried enumerating through the array but I end up with an Array index out of range error, possible due to the reshifting that occurs when deleting an item from the array.
for (index, value) in connectionTypeIDs.enumerate() {
if (value == connectionTypeToDelete){
connectionTypeIDs.removeAtIndex(index)
}
}
Any ideas on how to accomplish this?
The quickest way is to use filter. I don't know is that answer your question but you can have a look on this:
// remove 1 from array
arr = arr.filter{$0 != 1}

Count occurrences on a array using MATLAB [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Determining the number of occurrences of each unique element in a vector
I've the following array:
v = [ 1 5 1 6 7 1 5 5 1 1]
And I need to count the values and show the number that has more appearances.
From the example on the top, the solution would be 1 (there are five 1's)
Thanks in advance
Use mode.
If you need to return the number of elements as well, do the following:
m = mode(v);
n = sum(v==m);
fprintf('%d appears %d times\n',m,n);
Another method is using the hist function, if you're dealing with integers.
numbers=unique(v); %#provides sorted unique list of elements
count=hist(v,numbers); %#provides a count of each element's occurrence
Just make sure you specify an output value for the hist function, or you'll end up with a bar graph.
#Jacob is right: mode(v) will give you the answer you need.
I just wanted to add a nice way to represent the frequencies of each value:
bar(accumarray(v', 1))
will show a nice bar diagram with the count of each value in v.

Resources