-1

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.

user22485
  • 281
  • 1
  • 9
  • 1
    Hi Thomas, welcome to SO. Can you clarify what the desired output is? – Paolo Aug 23 '18 at 09:07
  • 1
    What would be the exact output you are looking for here? You are more likely to get useful help if you edit this information into your question as it is not very clear in its current form. – etmuse Aug 23 '18 at 09:07
  • 1
    Welcome to Stack Overflow! Please take the [tour] and read up on [ask]. The most important thing you'll learn there is *show your effort*. This helps both you and us, since we will know where exactly you're stuck and you are more likely to get help with specific issues than broad, open-ended questions like this. Please [edit] the question to show your (non-working) attempt, preferably in the form of code, this is called a [mcve]. – Adriaan Aug 23 '18 at 09:29

1 Answers1

1

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
Nicky Mattsson
  • 3,052
  • 12
  • 28