Using a for loop to generate multiple arrays with variables

I am trying to use a for loop to generate multiple arrays using a linspace function. So the data I have imported looks like this. I should also say that I did this through importing a spreadsheet so the volume=cellVectors(:,1); minflow rate=data(:,2); and max flow rate =data(:,3);
Volume Mix Max
.25 .0015 5000
.50 . .0018 8000
I am trying to create a linspace for each of the volumes starting from zero and going the max with about 1000 points in between but I want to use a loop or another function to create them all. Then use those arrays and substitute them into different functions (Engineering equations). The linspace should be linspace(0,max,100) for each volume (there are actually about 15 of them) but I don't know how to put it in a loop or store them.

Answers (3)

A loop is not necessary and is a waste of time:
A = [0.25 0.0015 5000
0.50 0.0018 8000];
%R2016b or later:
v = linspace(0, 1, 100) .* A(:, 3); %R2016b or later
%earlier versions:
v = bsxfun(@times, linspace(0, 1, 100), A(:, 3));

4 Comments

True. However OP specifically asked for a loop.
Here is the actual data I am using. So as an example for 0.5uL I want an array from 0 to 93.992 and then repeat that all the way down.I have already imported this spreadsheet into Matlab.
An image of a spreadsheet doesn't help us to know what form the data takes in matlab. Nonetheless, replace A(:, 3) above by your maximum flow rate column, whatever form that takes.
v = linspace(0, 1, 100) .* column_of_maximumflowrate
@Steven Mancini: screenshots are not useful to us: we cannot load them, we cannot test them, we cannot run them, we cannot import them, we cannot edit them, we cannot search them. They might look pretty, but they are not a way of transferring data. If you want help then upload your actual data file.

Sign in to comment.

In case you have R2016a or earlier and not assuming that max_flow or min_flow are column vectors:
bxsfun(@plus, min_flow(:), bsxfun(@times, (max_flow(:) - min_flow(:)), repmat(linspace(0, 1, NumberOfPoints), 16, 1)) )
In R2016b and later this, and assuming max_flow and min_flow are column vectors, this can be simplified to
min_flow + (max_flow - min_flow) .* linspace(0, 1, NumberOfPoints)
Hi guys,
I have almost the same issue. I am new with matlab ...
I wanna know how to loop through different variables stored in multiple vectors?
Example:
vector_variable_1 = xlread("read from excel file, 'sheet_name', 'A1:A10');
vector_variable_2 = xlread("read from excel file, 'sheet_name', 'B1:B10');
vector_variable_3 = xlread("read from excel file, 'sheet_name', 'C1:C10');
func(A(i), B(i), C(i)) = A*B + 3*C;
So, the objective is to calculate the result of a vector D, for each rows of data :
(i.e., for row 1, func(A,B,C) = A(1)*B(1) + 3*(C(1))
for row 2, func(A,B,C) = A(2)*B(2) + 3*(C(2))
...
for row n, func(A,B,C) = A(n)*B(n) + 3*(C(n))

2 Comments

Franck,
It would be best if you posted your inquiry as a new question rather than a comment or answer to an existing question.
Regarding syntax for your desired vector D, it would look like this:
D = A .* B + 3 * C;
The .* indicates element-wise multiplication of the arrays and requires that they be the same size (i.e., both n x 1 or 1 x n). Element-wise multiplication is not necessary for 3 * C since 3 is a scalar.
Also, the input function is xlsread, not xlread.
I seem to recall that xlread is File Exchange

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 23 Jan 2018

Commented:

on 29 Nov 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!