-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" ?

4
  • How is this different from you r previous question? Commented Oct 12, 2015 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 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. Commented Oct 12, 2015 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. Commented Oct 12, 2015 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... Commented Oct 12, 2015 at 14:48

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

2 Comments

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
data=[];vector = reshape(M.',[],1) ; vector = vector' % change from columns into 1 row ; data(i,:)= vector

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.