I have a ClosedRange from 1 to 10, I wanted to know can we remove some element of it? like removing 5 and 7 from that closedRange then we should have: 1 2 3 4 6 8 9 10 instead of having 1 2 3 4 5 6 7 8 9 10
let closedRange: ClosedRange<Int> = 1...10
It ends up being an array of type [ClosedRange<Int>.Element] because once you remove elements, it's not a range any more (since by definition it doesn't include all the elements between the bounds).
From the Apple docs (https://developer.apple.com/documentation/swift/closedrange):
An interval from a lower bound up to, and including, an upper bound.
let closedRange: ClosedRange<Int> = 1...10
let closedRangeArrayWithElementsMissing = closedRange.filter { $0 != 5 && $0 != 7 }
// = [1, 2, 3, 4, 6, 8, 9, 10]
Like the other answer states, no. symmetricDifference is good for this.
([5, 7] as Set).symmetricDifference(1...10)
Related
im a beginner here. Ive been stuck on a problem for some time now. Practicing in playground and i need to make a multiplication table.
basically, if i input 3, i want the table to read
1 2 3
2 4 6
3 6 9
Im confused on the loop for this though. Any help please?
Code so far
var x = 3
var width = 1
for x in 1...x {
for width in 1...width {
print(x, width*2)
}
}
this code prints
1 2
2 2
3 2
You could do it like this.
func multiplicationTable(till limit: Int) {
for i in 1...limit {
for j in 1...limit {
print(i * j, terminator: "\t")
}
print("")
}
}
multiplcationTable(till: 5)
Output
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
If conciseness is paramount:
let x = 3
let range = 1...x
for i in range {
print(range.map { String(i * $0) }.joined(separator: "\t"))
}
You can store the multiplication table in a 2D array of Ints. First, you can populate the first row and first column with numbers from 1 to the size of the multiplication table. Then for each element in the remaining empty positions, you just need to multiply the first element of the same row and the first element of the same column that the element resides in.
func multiplicationTable(ofSize n:Int) -> [[Int]] {
var table = Array(repeating: Array(repeating: 0, count: n), count: n)
table[0] = Array(1...n)
for i in 1..<n {
table[i][0] = i+1
for j in 1..<n {
table[i][j] = table[i][0] * table[0][j]
}
}
return table
}
multiplicationTable(ofSize: 5).forEach { row in
print(row,"\n")
}
Output:
[1, 2, 3, 4, 5]
[2, 4, 6, 8, 10]
[3, 6, 9, 12, 15]
[4, 8, 12, 16, 20]
[5, 10, 15, 20, 25]
I'd like to index matrix
x=[1:5;6:10]
x =
1 2 3 4 5
6 7 8 9 10
using array
[1,2,1,2,1]
to get
1 7 3 9 5
I tried this:
x([1,2,1,2,1],:)
ans =
1 2 3 4 5
6 7 8 9 10
1 2 3 4 5
6 7 8 9 10
1 2 3 4 5
but that is not what I want. Please help
I'd use linear indexing with sub2ind:
>> v = x(sub2ind(size(x),a,1:5))
v =
1 7 3 9 5
Let
ind = [1, 2, 1, 2, 1];
offset = [1:size(x, 1):numel(x)] - 1;
then
x(ind + offset)
returns what you want. This assumes that your index vector has an entry for every column of x and uses linear indexing to add a column offset to every in-column index.
What is the fastest and the simplest way to generate an array like
[0, 1, 3, 4, 6, 7, 9, 10, ...]
in MATLAB?
You can obtain the cumulative sum of the vector of steps (in your case it is [1 2 1 2 1 2 1 2 ...]). For example:
x = cumsum([0, repmat([1 2], 1, 4)])
x =
0 1 3 4 6 7 9 10 12
You can generate matrix with two rows: top row for odd array elements, bottom row for even elements. Than transform matrix into array with reshape.
>> a=[0:3:15; 1:3:16]
a =
0 3 6 9 12 15
1 4 7 10 13 16
>> a=reshape(a,1,12)
a =
0 1 3 4 6 7 9 10 12 13 15 16
Not one line but will work for either an odd or even number of total elements, and could be expanded if you wanted more than two different steps:
a = zeros(1,8);
a(1:2:end) = 0:3:10;
a(2:2:end) = 1:3:10;
Here is a simple and compact way:
A = 0:20;
A(3:3:end) = []
Here is a simple double array:
array=[3 1 1]
Largest element index is 1
or:
array=[3 9 1]
Largest element index is 2
How can I get the largest element index?
Use the second output argument of the max function:
[ max_value, max_index ] = max( [ 3 9 1 ] )
My standard solution is to do
index = find(array == max(array), 1);
which returns the index of the first element that is equal to the maximum value. You can fiddle with the options of find if you want the last element instead, etc.
If you need to get the max value of each row you can use:
array = [1, 2, 3; 6, 2, 1; 4, 1, 5];
[max_value max_index] = max(array, [], 2)
%3, 3
%6, 1
%5, 3
In Octave If
A =
1 3 2
6 5 4
7 9 8
1) For Each Column Max value and corresponding index of them can be found by
>> [max_values,indices] =max(A,[],1)
max_values =
7 9 8
indices =
3 3 3
2) For Each Row Max value and corresponding index of them can be found by
>> [max_values,indices] =max(A,[],2)
max_values =
3
6
9
indices =
2
1
2
Similarly For minimum value
>> [min_values,indices] =min(A,[],1)
min_values =
1 3 2
indices =
1 1 1
>> [min_values,indices] =min(A,[],2)
min_values =
1
4
7
indices =
1
3
1
Imagine I have a cell array "list of lists" in Octave:
octave:6> a = {[1], [3,4], [5,6,7], [8,9,10,11]}
a =
{
[1,1] = 1
[1,2] =
3 4
[1,3] =
5 6 7
[1,4] =
8 9 10 11
}
Now I want to extract a given element from each of the nested rows and the index of each of them is given in a list. E.g. [1, 2, 2, 3] would mean return [1, 4, 6, 10].
What is the best Octave-ish way to do this? I know how to do that with a loop, but that seems ugly...
It seems I have found the solution that is good to me. I realized that cellfun() takes a number of arguments so I can perform the element-wise mapping easily.
octave:31> cellfun(#(x,y) x(y), a, {1,2,2,3})
ans =
1 4 6 10