Storing Arrays in a Matrix

24 次查看(过去 30 天)
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.

采纳的回答

Paul
Paul 2023-4-30
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]
ans = 6×1
1 2 3 4 5 6
[x,y]
ans = 3×2
1 4 2 5 3 6
  2 个评论
Edoardo Modestini
Edoardo Modestini 2023-4-30
Hi, is there any way to have the arrays stored as arrays, eg if i called [x,y](1) it would display [1,2,3]?
Stephen23
Stephen23 2023-4-30
编辑:Stephen23 2023-4-30
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Call Python from MATLAB 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by