How to divide an array into separate arrays when the division concerns a proportion of the cell?
2 views (last 30 days)
Show older comments
Hello,
I have a flow passing through a trough of 24.5 mm. The trough is divided into nb = 10 number of virtual streams (so the length of each virtual stream is Lb = 2.45 mm). The flowrate within each virtual stream is also known. The flow at the end of the trough is divided into n number of flows by n-1 number of cutters. n might vary in different applications. The cutters position x across the trough might also vary. I need to simulate the cutters actions by calculating the proportion of the flow directing to each separate flow. The below figure shows the concept. The below array is the trough of 10 virtual streams and the digits in each cell shows the flowrate. The red sections show the width of the separated flows at the end of the trough. For instance, the first left flow contains the first plus ¾ of the second virtual streams and so on.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/285872/image.png)
I tried to write the below code to calculate each separate flow and it works, but I am searching a more intelligent generic code. I was wondering if you could help me improve it.
clc;
clear;
close all;
D = [1.1 1.2 1.4 1.7 2.0 2.3 2.7 3.1 3.6 4.2];
nb = 10;
Lb = 2.45;
x = [0;4;7;11;16;24.5];
L = x(2:end)-x(1:end-1);
n = floor(L ./ Lb); % Number of virtual streams that report completely to a separate flow
p = (L(1:end-1) - n(1:end-1) .* Lb) ./ Lb; % Proportion of the virtual stream that PARTIALLY reports to a separate flow
z(1,:) = [ones(1,n(1)), p(1), zeros(1,nb-n(1)-1)];
z(2,:) = [zeros(1,n(1)) 1-p(1) ones(1,n(2)), p(2), zeros(1,nb-n(1)-n(2)-2)];
z(3,:) = [zeros(1,n(1)+n(2)+ceil(p(1)+p(2))) 1-p(2) ones(1,n(3)), p(3), zeros(1,nb-n(1)-n(2)-n(3)-3)];
z(4,:) = [zeros(1,n(1)+n(2)+n(3)+ceil(p(1)+p(2)+p(3))) 1-p(3) ones(1,n(4)), p(4), zeros(1,nb-n(1)-n(2)-n(3)-n(4)-4)];
f = z .* repmat(D,length (L(1:end-1)),1);
f(5,:) = D - sum(f);
F = sum(f,2);
1 Comment
Stephen23
on 20 Apr 2020
Edited: Stephen23
on 20 Apr 2020
Your code appears to allocate the cutter-locations incorrectly. For example, given ten streams with eleven edges:
>> linspace(0,24.5,nb+1) % 11 edges for 10 streams
ans =
0.00000 2.45000 4.90000 7.35000 9.80000 12.25000 14.70000 17.15000 19.60000 22.05000 24.50000
% | str 1 | str 2 | str 3 | str 4 | str 5 | str 6 | str 7 | str 8 | str 9 | str 10 |
and your cutter positions:
x = [0;4;7;11;16;24.5]
it is clear that the cutters at positions 4 & 7 fall within streams two and three respectively (stream edges 2.45, 4.9, 7.35). But your scaling matrix z shows these cutters over three streams (not two), as shown on the second row of z:
>> z
z =
1.00000 0.63265 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.36735 1.00000 0.22449 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.77551 1.00000 0.63265 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000 0.36735 1.00000 1.00000 0.04082 0.00000
% str 1 | str 2 | str 3 | str 4 | str 5 | str 6 | str 7 | str 8 | str 9 | str 10
The cutter at position 7 clearly is not within the fourth stream, which only starts at position 7.35, so based on your explanation it would be erroneous to include any of the fourth stream to be in the flow between cutters 2 and 3. But according to your z matrix, you count part of the fourth stream in the 2nd flow.
Another indication that something is not quite right is in matrix f:
>> f
f =
1.10000 0.75918 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.44082 1.40000 0.38163 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 1.31837 2.00000 1.45510 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000 0.84490 2.70000 3.10000 0.14694 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 3.45306 4.20000
% str 1 | str 2 | str 3 | str 4 | str 5 | str 6 | str 7 | str 8 | str 9 | str 10
The last row is for the fifth flow, i.e. between the cutters at 16 & 24.5. But the cutter at position 16 falls within the seventh stream, not the ninth stream as your matrix indicates (the seventh stream goes from 14.7 to 17.15, which clearly includes the position of the cutter at position 16).
Accepted Answer
Stephen23
on 20 Apr 2020
Edited: Stephen23
on 20 Apr 2020
% data:
D = [1.1,1.2,1.4,1.7,2.0,2.3,2.7,3.1,3.6,4.2];
nb = 10;
Lb = 2.45;
x = [0;4;7;11;16;24.5];
% code:
Lw = linspace(0,24.5,nb+1) % stream edges
ida = x(1:end-1)<=Lw(2:end) & x(2:end)>=Lw(1:end-1) % any
idl = x(1:end-1)>Lw(1:end-1) & x(1:end-1)<Lw(2:end) % left
idr = x(2:end)>Lw(1:end-1) & x(2:end)<Lw(2:end) % right
K2 = ida + idr.*(x(2:end)-Lw(2:end))./Lb + idl.*(Lw(1:end-1)-x(1:end-1))./Lb % scale
F2 = sum(D.*K2,2)
Giving:
F2 =
1.8592
1.6408
2.8796
4.7531
12.1673
This code requires MATLAB >=R2016b, for earlier versions use bsxfun where required.
More Answers (0)
See Also
Categories
Find more on Data Type Identification in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!