Storing Arrays in a Matrix
27 views (last 30 days)
Show older comments
Hi, I'm currently trying to make a function that outputs an array containing all the different arrays for a stage of separations:
function stage_out = calc_stage(frac, ratio, Pin, Pout, perm, flux, total)
stage_k = calc_K(frac, flux, Pin, Pout);
stage_rej = calc_rej(frac, ratio, stage_k);
stage_perm = calc_perm(stage_rej, stage_k);
flow_perm = total*ratio;
flow_rej = total-flow_perm;
stage_perm_flow = stage_perm*flow_perm;
stage_rej_flow = stage_rej*flow_rej;
stage_out = [stage_k; stage_rej; stage_perm; stage_rej_flow; stage_perm_flow];
end
Stage_k ... etc are all 5x1 arrays, however when I call this function it outputs a single column array with a mixture of numbers in it. Coming over from Python, I'm not quite sure how this would work. How would I get this code to work as intended?
Thanks,
Edo
Edit: I fixed this issue, obviously changing the semicolons to colons helped. I was also using the wrong variable somewhere so this was also causing issues! Sorry for the bother.
0 Comments
Accepted Answer
Paul
on 30 Apr 2023
Hi Edoardo,
The ; concatenates the elements vertically. Use a , to concatenate horizontally
x = (1:3).'; y = (4:6).'; % example 3 x 1 vectors
[x;y]
[x,y]
2 Comments
Stephen23
on 30 Apr 2023
Edited: Stephen23
on 30 Apr 2023
See also the section "Matrices and Arrays" here:
"is there any way to have the arrays stored as arrays, eg if i called [x,y](1) it would display [1,2,3]?"
In MATLAB nearly everything is an array, as the documentation explains:
Numeric arrays contain numeric values stored in contiguous memory. Python itself does not have anything like that (for contiguous numeric arrays you have to use numpy or some other extension). In contrast, the closest equivalent to Python's container types list and tuple is a cell array:
So if you really want to nest lots of separate vectors in a container array just like poor old Python, then by all means use a cell array. However writing MATLAB code as if it were Python is very unlikely to deliver good code, as you will miss out on the benefits of working with contiguous numeric data, e.g.:
MATLAB is not Python.
More Answers (0)
See Also
Categories
Find more on Call MATLAB from Python 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!