This question already has answers here:
Exclude elements from array [duplicate]
(3 answers)
Closed 6 years ago.
I have a matrix <1x1000> containing integers. It contain the value 150 a couple of times and I want to remove that value completely. Any ideas how to?
Help is much appreciated!
If you want to remove all elements that equal 150 then
M = M(M ~= 150)
If you want to remove all elements belonging to a list of undesired numbers then
list = [150, 230, 420]
M = M(~ismember(M, list))
Same but different expression
M(M==150)=[];
list = [150,230,420];
M(ismember(M,list))=[];
When you type A(index)=[], it delete A(index). For example,
A = [1,2,3];
A(2) = [];
Then
A = [1,3]
Related
This question already has answers here:
Removing duplicate elements from an array in Swift
(49 answers)
Closed 5 years ago.
Say I have a set [A, B, B, C, D], how would I remove just the first B?
If charToDelete = B and I do this:
SlidingWin.remove(charToDelete)
won't it remove all of the B characters?
In Swift lingo, what you have is an array (not a set). If you want to remove the first "B" from an array, you can do this:
if let index = array.index(of:"B")
{ array.remove(at:index) }
[EDIT] example of a functional approach for anagrams:
let set1 = "cabb"
let set2 = "cbabeijbbacbkiie"
let anagrams = zip(set2.indices,set2.indices.dropFirst(set1.count-1))
.map{set2[$0...$1]}
.filter{$0.sorted() == set1.sorted()}
This question already has answers here:
Function for 'does matrix contain value X?'
(4 answers)
Closed 5 years ago.
I have a table that has a column containing values between 1 and 20. I want to filter the table such that I can show only certain discrete values, i.e. 3, 10, 12, and 19. The problem is writing this is cumbersome, especially since I want to write other criteria for filtering too.:
i = tb.subn==3 | tb.subn==10 | tb.subn==12 | tb.subn==19
If I use i = tb.subn==[3 10 12 19] then I get a :x4 boolean matrix. How can I get that into just one column?
If it were &, I suppose I could use prod(tb.subn==[3 10 12 19],2) but I can't figure out or.
Thanks.
You can use ismember(tb.subn, [3, 10, 12, 19])
i = all(tb.subn==[3 10 12 19], 2);
Note that this works only for very late matlab, while you solution will work better for all versions.
This question already has answers here:
Removing Duplicates From Array of Custom Objects Swift
(4 answers)
Remove objects with duplicate properties from Swift array
(16 answers)
Closed 5 years ago.
I have an array
Contract object contains:
String:id
String:value
Array is:
contract1 = Contract.new()
contract1.id = 2
contract1.value = "Apple"
contract2 = Contract.new()
contract2.id = 2
contract2.value = "Pen"
contract3 = Contract.new()
contract3.id = 1
contract3.value = "Pineapple"
array = [Contract1, Contract2, Contract3]
I would to find out the list of contracts whose IDs are distinct.
I want to have a solution that doesn't make me change the implementation of my object (overriding the isEqual method etc) since I will be using it for more than one object through out my code.
Desired result:
[contract1, contract3] or [contract2, contract3]
Ideally, an extension with additionally a method to only return the values that are being made distinct:
Desired result: [2, 1]
I tried a couple of approaches from similar questions but either the answers are outdated or doesn't fit my need.
This question already has answers here:
How do I compare all elements of two arrays?
(3 answers)
Closed 6 years ago.
I have two matrices: A=[1,2,3,4,5] and B=[1,2,3,4,6]. I need to compare elements of those matrix, and as a result I need to have binary matrix
Result=[1,1,1,1,0], that means if A(i)==B(i) then Result(i)=1 else Result(i)=0.
I have tried with:
if (isequal (A,B))
Result=1
else
Result=0
end
I have tried: Result=sign(A,B)
I hope that you could help me please?
A = [1,2,3,4,5]
B = [1,2,3,4,6]
Result = A == B
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How many elements have a name longer than 10 characters in xlsx matlab?
I tried this.
[number cities] = xlsread('weather.xlsx', 'city')
if cities{1}<=char(10);
x+1=x;
Your code above only checks to see if the first city has less than 10 characters. Also, char(10) does not make any sense. You are checking to see if the string contained in cities{1} is less than or equal to the character array containing 10.
Because cities is in a cell array, I would use cellfun to first return the length of each city. Then you can use a sum as well as a Boolean condition to help you figure out how many there are that have more than 10 characters.
As such, here is the code:
A = cellfun(#length, cities);
numCitiesMoreThan10 = sum(A > 10);
Here is an example (simulated):
cities = {'New York', 'Los Angeles', 'Toronto', 'Ottawa', 'Sydney', 'Melbourne', ...
'Timbuktu', 'Singapore', 'Mississippi'};
A = cellfun(#length, cities);
numCitiesMoreThan10 = sum(A > 10);
>> numCitiesMoreThan10 =
2
This makes sense, as the only two cities with more than 10 characters (including spaces) are Los Angeles and Mississippi.
Aside
Just learned that cellfun has something built-in that can do this. You can also do:
A = cellfun('size', cities, 2);
This accesses each element in the cell array (cities), and returns the size of whatever dimension you specify in the last parameter of cellfun when you call it with the size parameter. You can also chain more than one cell array together. The reason why you are choosing the third parameter as 2 is because each string in the cell array is a 1 x N array. As such, we need to read how many columns there are so that this equates to the length of each string.