Create matrices from a given cell-array of strings with different lengths

一个人想着一个人 提交于 2019-11-29 12:54:25

Firstly you need to work out how to do this just for a single sequence:

  1. Create a matrix of random numbers between 0.5 and 1:

    M = 0.5*rand(4) + 0.5;
    
  2. Set the main diagonal to equal 0.5

    M(logical(eye(4))) = 0.5;
    
  3. Set the upper triangle of M equal to 1 - the lower triangle:

    M(triu(true(4))) = 1 - M(tril(true(4)));   %// Note the main diagonal doesn't matter...
    
  4. Work out which letter is missing and set the row and column equal to 0.5 accordingly:

    fullSeq = 'abcd';
    idx = find(fullSeq == setdiff(fullSeq, 'abd'));
    %// at this point you'll need to check if idx is empty first...
    M(:,idx) = 0.5;
    M(idx,:) = 0.5;
    

And now that you can do it for one matrix, just loop over your cell array or else encapsulate this into a function and use cellfun.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!