Storing Arrays in a Matrix

23 visualizaciones (últimos 30 días)
Edoardo Modestini
Edoardo Modestini el 30 de Abr. de 2023
Editada: Stephen23 el 30 de Abr. de 2023
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.

Respuesta aceptada

Paul
Paul el 30 de Abr. de 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]
ans = 6×1
1 2 3 4 5 6
[x,y]
ans = 3×2
1 4 2 5 3 6
  2 comentarios
Edoardo Modestini
Edoardo Modestini el 30 de Abr. de 2023
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 el 30 de Abr. de 2023
Editada: Stephen23 el 30 de Abr. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Call Python from MATLAB en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by