MATLAB: Is it possible to form a matrix with strings and manipulate them

MATLABmatricesmatrixmatrix arraymatrix manipulationscheduling

Can I make a matrix like [(a,b), (c,d), (d,f) ; (t,a), (a,b), (g, a); (a,f),(c,d),(a,g) ] and then reorder the rows based on the maximum number of same strings (say a ) in the matrix? the answer will be [(t,a), (a,b), (g, a); (a,f),(c,d),(a,g) ; (a,b), (c,d), (d,f)]

Best Answer

  • Try this, it's using cell arrays. However I believe that in the newest versions of matlab cell arrys that include Sting work in a different way.
    m={['a','b'], ['c','d'], ['d','f'] ; ['t','a'], ['a','b'], ['g', 'a']; ['a','f'],['c','d'],['a','g']}
    counter=zeros(size(m,1),1);
    for i=1:size(m,1)
    for j=1:size(m,2)
    counter(i)=counter(i)+length(strfind(m{i,j},'a'));
    end
    end
    [~,ind]=sort(counter,'descend');
    result=m(ind,:)
    hope it helps