How to delete an element from an array, which is in looping ? - loops

Following is a psuedo-code example of what I am trying to do. I loop over an array and delete elements based on a condition. Now the problem is when I remove the elements the size of myArray is changing, and I get an out of index exception. Any ideas on how to get around this?
for i = 1:size(myArray)
if myArray(i) == someValue
remove_from(myArray, i)

a simple solution for this problem is loop from the last to the first element.
for i = size(myArray):1
if myArray(i) == someValue
remove_from(myArray, i)

Related

How to remove rows from a struct based on a condition?

I want to delete rows from a struct using a condition which is if hsvmean field = 0 then delete the row. I tried this code:
for j =1:length(selectedIms) % the length of selectedIms is 100
if selectedIms(j).hsvmean == 0
selectedIms(j)=[];
end
end
But I get this error:
Index exceeds the number of array elements (73).
Error in HSVColorSelection (line 57)
if selectedIms(j).hsvmean == 0
How can I delete these items?
Here’s a solution in case someone needs it. Create a new struct in which there would only be the filtered rows
newSelectedlms = meanValues([meanValues.selected_ims]~= 0);
You’re deleting elements, making the array smaller, as you loop over the elements. The end index you loop over is determined at the start of the loop, hence you will index past the end of the array. You are also skipping over elements.
One simple solution is to loop starting at the back:
for j=length(selectedIms):-1:1
...
end
The other solution is to delete all elements at once. You could build a list of indices to delete in the loop, then delete those elements after the loop. Or do it without a loop:
selectedIms([selectedIms.hsvmean] == 0) = [];

MATLAB: deleting elements from multi-level cell

I've got a large multi-level cell and I'm trying to delete parts of it within a for loop but can't get past "Error: The expression to the left of the equals sign is not a valid target for an assignment."
Have tried a few versions and keep hitting the same error.
for i=1:length(List{1}{17}{imagenum})
if List{1}{17}{imagenum}(i)>1
List{1}{17}{imagenum}(i)=[];
end
end
Separate question: When I delete parts of a cell/anything within a for loop, is there a standard/smart way of making sure the for loop stays on track? I've been using a variable to keep track of deletions but I'm assuming there's a smarter way of doing this:
deletions=0;
for i=1:length(List{1}{17}{imagenum})
if List{1}{17}{imagenum}(i-deletions)>1
List{1}{17}{imagenum}(i-deletions)=[];
deletions=deletions+1;
end
end
Let's assign List{1}{17}{imagenum} to a:
a = List{1}{17}{imagenum};
Is 'a' cell or array? If it is array:
for i = numel(a):-1:1
if a(i)>1, a(i) = []; end
end
Easier way should be:
a(a>1) = [];
If it is cell:
for i = numel(a):-1:1
if a{i}>1, a(i) = []; end
end
General rule: when you delete items in a loop, you start from the end, not the beginning.

How would one access each element in a mulidimensional array without using #flatten?

I already flunked an assessment a little while ago but this question is still bugging me. The idea is to add all the elements of a multidimensional array like this: [1,[2,3],4,[5,6,[7]]]] and add them up as if they were all one-line, like 1+2+3+4+5+6+7, without being able to use #flatten. I got about as far as removing all the integers (like 1 and 4) and adding them into its own variable and then trying to reduce(:+) the 2nd level arrays( [2,3], etc), but I can't figure out how to write a loop that will delve deeper into the dimensions, like the [7]. How would one do that?
Code I've tried thus far
def multi_array_sum(arrays)
#need to add up all array elements
num = 0
arr = {}
arrays.each {|int| num = num + int if int.class == Fixnum; arrays.delete(int)}
array.map do |numz|
num = num + numz.reduce(:+)
end
end

Is there a more efficient way of choosing an element from 1 array and to every element of the same array

I want to, for every element of an array ZAbs, compare it for equality to every element of the array itself and put them into another distinct array. I want the distinct array's elements to have the same index as the ZAbs array.
I did this by creating 4 nested for loops:
for pAbs2 = 1:400
for qAbs2 = 1:300
zAbsCompare = ZAbs(qAbs2, pAbs2);
for pAbs3 = 1:400
for qAbs3 = 1:300
zAbsCompare2 = ZAbs(qAbs3, pAbs3);
if (zAbsCompare == zAbsCompare2)
InitialZModEqualsImag(pAbs2,qAbs2) = InitialZImag(qAbs2, pAbs2);
InitialZModEqualsReal(pAbs2,qAbs2) = InitialZReal(qAbs2, pAbs2);
end
end
end
end
end
However, this runs really slowly. I can't think of a better way to do this, but as I'm inexperienced with MATLAB there's probably something I'm overlooking here. Any help?
EDIT: Fixed an error and restated the question.
You can do the comparison (not sure that's what you want) efficently with bsxfun:
comp = bsxfun(#eq, X, shiftdim(X,-2));
The result comp(m,n,p,q) is 1 if X(m,n) == X(p,q), and 0 otherwise.

VBScipt Array Comparison

i have a problem with VBScript.
I am trying to loop through an array to compare all the values match.
I.e i have a tring array like the one below. i want to compare each of the values match using vbscript.
tmp(0) = "12345"
tmp(1) = "12345"
tmp(2) = "12345"
tmp(3) = "12345"
tmp(4) = "12345"
If i loop over the array i will have to do this twice in order to compare the vals. But how can i handle the first values. If the first value is wrong then its never picked up as both arrays are identical. I do not know how to get around this problem. Could someone please advise.
for x=0 to UBound(tmp)
for each val in tmp
if ( tmp(x) <> val)
print (mismatch)
End if
Next
Next
Not sure if I understand your question correctly. Do you want to check if all values of an array are equal? If so, something like this should do:
elementsEqual = True
For i = 1 To UBound(tmp)
If tmp(i) <> tmp(0) Then
elementsEqual = False
Exit For
End If
Next
You don't need to compare each element with each other element to check if all are equal. If not all elements are equal, then one of them will be inequal to the first element, so you just need a single loop.

Resources