## Multiple arrays in a nested for loop?

## Multiple arrays in a nested for loop?

(OP)

Hey guys, I've tried searching for this but haven't been able to find it.

It's been a little since I've done anything relevant in MATLAB and I've run into a little problem with my code. My problem is that I want a program to grab an input for a number of vectors and then I want that program to fill out those vectors based on a number given for a vector size. Let me try and clarify that. I want to calculate a result using 4 column vectors (4 is an arbitrary number given just as an example). I then want to be able to input values for each one of the elements in those vectors. So, I've got a for loop that runs from 1 to the upper bound of the number of vectors and within that loop I've got another for loop that runs from 1 to the upper bound of the vector size. So, in the end I'll end up with A1, A2, A3, A4, all the same size, but with values I enter for each element. A1 could look like [1;7;5;9], A2 like [5;7;5;1] and so on. Has anyone done anything like this before? I'll post my crummy code so you may have a better idea. I know the code doesn't wrong and I know it's at least a problem with my syntax. Thanks in advance!

function [numvectors] = MGS(numvectors)

check = isa(numvectors, 'numeric');

while check == 0;

numvectors = input('Please enter a number\n')

check = isa(numvectors, 'numeric');

end

vectorsize = input('Enter the size of the column vector (A x 1, where A is the size')

for i = 1:numvectors;

str = num2str(i);

for j = 1:vectorsize;

num = input ('Enter number j value for number i matrix')

A(str)(1, j) = num;

end

end

It's been a little since I've done anything relevant in MATLAB and I've run into a little problem with my code. My problem is that I want a program to grab an input for a number of vectors and then I want that program to fill out those vectors based on a number given for a vector size. Let me try and clarify that. I want to calculate a result using 4 column vectors (4 is an arbitrary number given just as an example). I then want to be able to input values for each one of the elements in those vectors. So, I've got a for loop that runs from 1 to the upper bound of the number of vectors and within that loop I've got another for loop that runs from 1 to the upper bound of the vector size. So, in the end I'll end up with A1, A2, A3, A4, all the same size, but with values I enter for each element. A1 could look like [1;7;5;9], A2 like [5;7;5;1] and so on. Has anyone done anything like this before? I'll post my crummy code so you may have a better idea. I know the code doesn't wrong and I know it's at least a problem with my syntax. Thanks in advance!

function [numvectors] = MGS(numvectors)

check = isa(numvectors, 'numeric');

while check == 0;

numvectors = input('Please enter a number\n')

check = isa(numvectors, 'numeric');

end

vectorsize = input('Enter the size of the column vector (A x 1, where A is the size')

for i = 1:numvectors;

str = num2str(i);

for j = 1:vectorsize;

num = input ('Enter number j value for number i matrix')

A(str)(1, j) = num;

end

end

## RE: Multiple arrays in a nested for loop?

num_arrays = input('Specify the number of arrays');

length_array = input('Specify the number of rows in the array');

for i = 1:num_arrays

for j = 1:length_array

temp = input(['Enter data for array ', num2str(i), ', element ', num2str(j), ':'], 's');

data(j,i) = str2num(temp);

end

end

for i = 1:num_arrays

eval([ sprintf('x%d = data(:,%d);', i,i)])

end

But, I've heard that using eval is a bad idea. Any reason as to why this is? If it is a bad idea to use eval, is there an alternate way to get the same result this code produces? Thanks in advance!

## RE: Multiple arrays in a nested for loop?

Try assignin.

Cheers

Greg Locock

New here? Try reading these, they might help FAQ731-376: Eng-Tips.com Forum Policies http://eng-tips.com/market.cfm?

## RE: Multiple arrays in a nested for loop?

for i = 1:num_arrays

x{i}=data(:,i)

end

This makes actually doing a transpose easier than trying to figure out how to get " ' " to read correctly within an eval statement. I also read that some times the MATLAB C compiler bugs out with the eval statement. Don't know if this is correct or not. Either way, thanks for the response!

## RE: Multiple arrays in a nested for loop?

## RE: Multiple arrays in a nested for loop?