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.
댓글 수: 0
채택된 답변
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]
[x,y]
댓글 수: 2
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 Center 및 File Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!