Lets Learn together... Happy Reading

" Two roads diverged in a wood, and I,
I took the one less traveled by,
And that has made all the difference "-Robert Frost

Number of occurrences in cell array

To find the number of occurrence of a string in an array:

Consider a cell array
c={'one';'two';'three';'nine';'five';'two';'six';'one'; 'two';'five';};


The number of times the string ‘one’ occurs in the array

sum(strcmp('one',c))

ans =

     2

To find the number of occurrence of all the elements in the array:


%c={'one';'two';'three';'nine';'five';'two'; 'six';'one'; 'two';'five';}; %dim=1

c='one','one','two','two','three','three','three','four','five','six','five','five'};%dim=2;

dim=2;
%Initialize the 'cell_array'
cell_array=cell([2 size(c,dim)]);
display(c);
inc=1;
for i=1:size(c,dim)
    %Compare the elements in the 'cell_array' with the elements in 'c'
   if(strcmp(cell_array(1,:),c(i))==0)
       %If the element is not present, then add it to 'cell_array'.
       cell_array(1,inc)=c(i);
       %Find the number of occurence of the element
     num=   sum(strcmp(c(i),c));
     cell_array(2,inc)=num2cell(num);
     inc=inc+1;
   else
       %Delete if the element is already present in the 'cell_array'.
       cell_array(:,inc)='';
      
      
   end
  
  
end

display(cell_array);



The result shows the elements in the first row and the number of occurrences in the second row.
like button Like "IMAGE PROCESSING" page

Array Search

Let's search the cell array and find the index. This simple example will help to find the index in cell array of large size.

names={'Hilda' 'Helen' 'Ada'; 'Matthew' 'Jenny' 'Justin'; 'Agnes' 'Merlin' 'Mark';}

names = 

    'Hilda'      'Helen'     'Ada'   
    'Matthew'    'Jenny'     'Justin'
    'Agnes'      'Merlin'    'Mark'  

I need to know whether the name 'Justin' exists in this cell array

find(strcmp(names,'Justin')==1)

ans =

     8

Let me retrieve the name 'Justin'  , using the index

names{8}

ans =

Justin


like button Like "IMAGE PROCESSING" page

Adding elements to a 2d array

A=[100 230 11; 22 10 15; 2 3 90;]

A =

   100   230    11
    22    10    15
     2     3    90

To add a row at the beginning:

A=[[10 20 30]; A]

A =

    10    20    30
   100   230    11
    22    10    15
     2     3    90

To add a row at the end:

A=[A; [10 20 30];]

A =

   100   230    11
    22    10    15
     2     3    90
    10    20    30


To add a column at the beginning:

A=[[10; 20 ;30] A]

A =

    10   100   230    11
    20    22    10    15
    30     2     3    90


To add a column at the end:

A=[A [10; 20 ;30]]

A =

   100   230    11    10
    22    10      15    20
     2     3        90    30


To add a row in the middle:


A=[10 12 14 16; 3 6 9 12; 4 8 12 16; 5 10 15 20;]

A =

    10    12    14    16
     3     6     9    12
     4     8    12    16
     5    10    15    20


A=[A(1:2,:); [8 16 24 32]; A(3:4,:);]

A =

    10    12    14    16
     3     6     9    12
     8    16    24    32
     4     8    12    16
     5    10    15    20

To add a column in the middle:


A=[A(:,1:2) [100; 200; 300; 400;] A(:,3:4)]

A =

    10    12   100    14    16
     3     6     200     9    12
     4     8     300    12    16
     5    10    400    15    20



like button Like "IMAGE PROCESSING" page

Array sorting in MATLAB

                 Sorting an array and retrieving the minimum and maximum values with the index may seem simpler but this helped me in many ways especially when working with arrays. So this is just a refresh on how to sort a 1 and 2d arrays.

Single dimensional array sorting

To sort the array and display the position (index):

A = [5 8 9 10 100 7 50 8 88 6;];

Before sorting:
A =   5     8     9    10   100     7    50     8    88     6

[sorted_array,pos]= sort(A)

After sorting: Here the sorted array and the array position of each value will be displayed.
By default, sorting type is ‘ascending’.


sorted_array =     5     6     7     8     8     9    10    50    88   100


pos =     1    10     6     2     8     3     4     7     9     5


To find the maximum value:
A =   5     8     9    10   100     7    50     8    88     6

[max_value,max_pos]=max(A)

max_value  =    100


max_pos  =      5

To find the minimum value:

A =   5     8     9    10   100     7    50     8    88     6


[min_value,min_pos]=min(A)

min_value =      5


min_pos =      1


Two dimensional array sorting:

B=[17 5 8 9; 45 23 50 6; 7 49 21 11]


Before sorting:

B =     17     5     8     9
           45    23    50     6
            7    49    21    11


[sorted_array, pos]=sort(B(:))

After sorting:

sorted_array =

     5
     6
     7
     8
     9
    11
    17
    21
    23
    45
    49
    50


pos =

     4
    11
     3
     7
    10
    12
     1
     9
     5
     2
     6
     8

Maximum value:
[sorted_array, pos]=max(B(:))

sorted_array =

    50


pos =

     8


Minimum Value:
[sorted_array, pos]=min(B(:))

sorted_array =

     5


pos =

     4

To retrieve the minimum value: B(4)
ans =      5
like button Like "IMAGE PROCESSING" page
Next Post Home
Google ping Hypersmash.com