Search code examples
matlabfor-loophypothesis-test

for loop group of numbers


I am trying to create a for loop that inserts a group of numbers,

I would like to insert what I have in the '' each time, here, three times.

for zz=['1 0 0 0', '0 1 0 0', '0 0 0 1'];
    H=zz
end

Any ideas would be appreciated.


Solution

  • You are thinking about correcly, however you have made the classical mistake of using '' in stead of "". The first is a character array the latter is a string. In other words,

    A = 'hello';
    

    corresponds to the vector of letters

    A = ['h','e','l','l','o'];
    

    Thus when you write

    zz=['1 0 0 0', '0 1 0 0', '0 0 0 1']  
    

    you concatenate the characters and obtain

    zz ='1 0 0 00 1 0 00 0 0 1';
    

    then running the for-loop runs through that vector first setting z='1', then z=' ' (space) and so on. What you want (i guess) is to put

    zz=["1 0 0 0", "0 1 0 0", "0 0 0 1"]
    

    which is the vector of the three strings "1 0 0 0", "0 1 0 0" and "0 0 0 1", thus your for-loop puts first zz="1 0 0 0" then z = "0 1 0 0" and finally zz ="0 0 0 1".

    In total

    for zz=["1 0 0 0", "0 1 0 0", "0 0 0 1"];
        H=zz
    end