-1

I have a string S1='ACD'. I generate the matrix based on the S1 as follows:

fullSeq = 'ABCD';
idx = find(fullSeq == setdiff(fullSeq, 'ACD')); % it is OK 
M(:,idx) = 0.5
M(idx,:) = 0.5
M(logical(eye(4))) = 0.5

The output is OK:

M =

0.5000    0.5000    0.2003    0.3279
0.5000    0.5000    0.5000    0.5000
0.8298    0.5000    0.5000    0.2452
0.7997    0.5000    0.7548    0.5000

Now, I would like to use a loop though the cell-array input-cell to generate 3 matrices (based on the above code) of the 3 strings in the cell-array as follows:

input_cell= {'ABCD','ACD', 'ABD'}


for i=1:numel(input_cell)



    M = 0.5*rand(4) + 0.5;

    M(triu(true(4))) = 1 - M(tril(true(4)));   

    fullSeq = 'ABCD';
    idx = find(fullSeq == setdiff(fullSeq, input_cell{i} )); % something wrong here

     M(:,idx) = 0.5
     M(idx,:) = 0.5
     M(logical(eye(4))) = 0.5

end

The error is :

 Error using  == Matrix dimensions must agree.

 Error in datagenerator (line 22)
 idx = find(fullSeq == setdiff(fullSeq, input_cell{i} ));   

How can I fix this problem to generate 3 matrices? Or any other solutions instead of using "for loop" ?

Stewie Griffin
  • 14,889
  • 11
  • 39
  • 70
kgk
  • 649
  • 1
  • 10
  • 23
  • How is this different from you r previous question? – Ander Biguri Oct 12 '15 at 14:36
  • 2
    `setdiff(fullSeq, input_cell{1} )` is `setdiff(fullSeq, 'ABCD')` which returns an empty matrix. I did specify in my [previous answer](http://stackoverflow.com/questions/33011701/create-matrices-from-a-given-cell-array-of-strings-with-different-lengths) to this question (which you should link to from here) that you will have to account for the case when `setdiff` returns an empty marix. I told you it would error there. – Dan Oct 12 '15 at 14:37
  • @Dan: Thanks for your comment. If input_cell= {'BCD','ACD', 'ABD'}, the code is OK. However, when I try to change the input_cell= {'CBAD','ACD', 'ABD'}, it is still an error there. – kgk Oct 12 '15 at 14:45
  • 1
    @kgk yes ofcourse, the issue is that `setdiff(fullSeq, 'ABCD')` (and note that the order of the letters does not matter!) returns an empty matrix. then comparing a string with an empty matrix using `==` is why you get the error. So call `letter = setdiff(fullSeq, input_cell{i})` before you call `idx = find(fullSeq == letter)`, and make sure that the second part is inside an `if` statement that screens for the empty case... – Dan Oct 12 '15 at 14:48

1 Answers1

1

Try changing

fullSeq = 'ABCD';
idx = find(fullSeq == setdiff(fullSeq, input_cell{i} )); % something wrong here
...

to this:

fullSeq = 'ABCD';
letter = setdiff(fullSeq, input_cell{i})
if isempty(letter)
    idx = find(fullSeq == letter);
    M(:,idx) = 0.5
    M(idx,:) = 0.5
end
M(logical(eye(4))) = 0.5

But also, you realise that you are just overwriting M at each iteration and never actually storing the past results right?

Dan
  • 45,079
  • 17
  • 88
  • 157
  • Thanks ! It works quite well. But it seems that it can handle when the length of each string is the equal to fullSeq. By the way, I also store the value of each matrix in the form of each vector in a new matrix as follows: vector = reshape(M.',[],1) vector = vector' % change from columns into 1 row data(i,:)= vector – kgk Oct 12 '15 at 14:58
  • data=[];vector = reshape(M.',[],1) ; vector = vector' % change from columns into 1 row ; data(i,:)= vector – kgk Oct 12 '15 at 14:59