Using a for loop to generate multiple arrays with variables
Show older comments
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.
1 Comment
Stephen23
on 23 Jan 2018
You might like to read this:
And then use indexing, exactly as Star Strider showed.
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
Star Strider
on 23 Jan 2018
True. However OP specifically asked for a loop.
Steven Mancini
on 23 Jan 2018
Edited: Steven Mancini
on 23 Jan 2018
Guillaume
on 23 Jan 2018
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.
Walter Roberson
on 23 Jan 2018
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)
Franck Chan
on 29 Nov 2019
0 votes
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
goc3
on 29 Nov 2019
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.
Walter Roberson
on 29 Nov 2019
I seem to recall that xlread is File Exchange
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!