3
i=1;  
while i:length(array)
    if (array(i)<=1000)
        array(i)=[];
    else
        i=i+1;
    end
end

This is a matlab code. can any body tell me how do i convert this into for loop.

3 Answers3

3

You actually do not need a loop; you can remove the element "<= 1000" by using the MatLab indexing properties:

array(array <= 1000)=[]

Edit to answer the question in the comment

remove other element from another array by indexing properties

To remove elements of another array that are in the same position of the ones you want remore in the first array, you have to store those position indexes and then use them to address the elements of the second array:

When you look for the indexes in the first array, you can evaluate them either as logical index or double index:

The instruction:

logical_idx=array <= 1000;

returns the array logical_idx which is of logical type, that is

-1 if the condition is verified

-0 if it is not

The function find can be used to "find" the element of an array meeting a condition:

double_idx=find(array <= 1000)

The array returned is, in this case, of double type.

When you use the logical index to access to the elements of an array, the selected ones are those corresponding to 1 in the logical array. As an example, if the logical index array is:

[0 1 1 0 0]

the second an the third elements will be selected.

When you use the double index, the access is made considering the position specified by the index. With respect to the previous example, the double version of the index array will be:

[2 3]

Once you have identified the index (by using one of the two above approaches) you can use them to access to the elements of different arrays.

Assuming you have three arrays:

  • array: the one in which you are looking for the elements <= 1000 to be removed

  • array_1: a second array from which you want to remove the elements in the same position as the ones removed from array

  • array_11: similar to array_1

this is an example in which the two approaches are used:

% Identify the logical indexes of the element to be removed
logical_idx=array <= 1000;
% Identify the integer indexes of the element to be removed
double_idx=find(array <= 1000);

% Remove the unwanted elements by "direct" indexing
array(array <= 1000)=[]
% Remove unwanted elements using logical indexes
array_1(logical_idx)=[]
% Remove unwanted elements using integer indexes
array_11(double_idx)=[]

End edit

If you want to use a for loop you need to create a new array and "reverse" the condition in the "if" in order to identify the element of the original array that have to be stored in the new arrray (the ones that ... you have not to discrard).

array_2=[];
cnt=0;
for i=1:length(array)
   if(array(i) > 1000)
      cnt=cnt+1;
      array_2(cnt)=array(i);
   end
end

Hope this helps.

Qapla'

il_raffa
  • 5,090
  • 129
  • 31
  • 36
  • il_raffa if i used option 1. Is it possible to remove the element <= 1000 and at the same index it also remove other element from another array by indexing properties. like this :i=1; while i:length(array) if (array(i)<=1000) array(i)=[]; array1(i)=[]; else i=i+1; end end – user3662189 May 09 '16 at 02:20
  • Happy I've been of use to you. I can provide you with an answer to you question (_remove other element from another array by indexing properties_) late today. – il_raffa May 09 '16 at 06:45
  • Here I'm. I've updated the answser to address your question (_remove other element from another array by indexing properties_). – il_raffa May 09 '16 at 18:59
1

Try this:

for i=1:1:size(array,1)*size(array,2)
    if array(i)<=1000
        array(i)=0;
    end
end
array = nonzeros(array)
Sardar Usama
  • 19,536
  • 9
  • 36
  • 58
0

You can use the following code to convert the code to a for loop.

for i=1:length(array)

   some code here
end

Looking at your code though, you probably want to rewrite this as a matrix operation. It looks like your code is removing anything less than 1000, in which case you should use MATLABs array indexing features in order to remove those values (see below).

array(array<1000)=[];

The solution will likely be significantly faster and easier to understand as well.

zglin
  • 2,891
  • 2
  • 15
  • 26