×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Multiple arrays in a nested for loop?

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
Replies continue below

Recommended for you

RE: Multiple arrays in a nested for loop?

(OP)
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!

RE: Multiple arrays in a nested for loop?

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: Eng-Tips.com Forum Policies  http://eng-tips.com/market.cfm?

RE: Multiple arrays in a nested for loop?

(OP)
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!

RE: Multiple arrays in a nested for loop?

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.

RE: Multiple arrays in a nested for loop?

(OP)
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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members! Already a Member? Login



News


Close Box

Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close