Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple arrays in a nested for loop?

Status
Not open for further replies.

mechanate

Mechanical
Mar 18, 2010
16
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
 
Replies continue below

Recommended for you

Okay, so I've got this far with the code:

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!
 
eval is bad because it allows you to do almost anything. As such lazy programmers tend to use it when they should be using safer alternatives. That being said I use it a fair bit.

Try assignin.



Cheers

Greg Locock


New here? Try reading these, they might help FAQ731-376
 
Haha, yeah, I got caught up in the ease of using it. I figured it out though using cell arrays. I didn't know you could do something like:


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!
 
Just a suggestion, but if all the arrays are the same length, why not use a 2d matrix with each column being one of the vectors. Cells might be fine for this case, but I've found cells to be cumbersome at times.
 
Well, I had considered that, but this is part of a code to work a modified Grahm-Schmidt (not sure if I spelled that right) process. I didn't see any way to perform the calculations after grabbing all the values without having individual arrays.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor