Finding integers between two vectors

hi
im new in the matlab world
i have 2 vectors A and B size (1xM)
i need to find all the integers betwwen corresponding Columns and store the integers in cell arry
for example:
A= 4.54 5.11 9.58 15.04
B= 1.01 3.2 5.5 16.98
i need to create cell arry X that each column contains all the integers between the numbers in A and B columns (1.01-4.54 the integers 2,3,4 3.2-5.11 the integers 4,5... )
X=
2 4 6 16
3 5 7
4 8
9
how can i do it?
thanks

 采纳的回答

A= [4.54 5.11 9.58 15.04];
B= [1.01 3.2 5.5 16.98];
C = sort([A; B]);
C(1,:) = ceil(C(1,:));
C(2,:) = floor(C(2,:));
temp = diff(C) + 1;
D = zeros(max(temp),size(C,2));
for ii = 1:size(C,2)
D(1:temp(ii),ii) = C(1,ii):C(2,ii);
end

3 个评论

have a more difficult problem and I would be happy if you help me..
i have 4 Matrices in UV axis :
uv1=[umin; vmin; ones(size(P1U))];% (umin,vmin)
uv2=[umax; vmax; ones(size(P2U))];% (umax,vmax)
uv3=[umin; vmax; ones(size(P3U))];% (umin,vmax)
uv4=[umax; vmin; ones(size(P4U))];% (umax,vmin)
Each column in each matrix contains a coordinate(integers) ..all four matrices containing in each column coordinates that build a block i need to find all the integer coordinates in Each block and arrange them in two matrices one for U and one for V, Each column in U corresponds to the column in V
U=
1 5 8
2 6 0
3 7 0
4 0 0
V=
2 5 7
3 6 0
4 7 0
8 0 0
thanks
@david, I expect you realize this, since you accepted the answer, but this solution has two features that are different from what you specified in your question:
  • It is a numeric array, not a cell array.
  • It includes extra zeros.
Maybe that doesn't matter to you, but just in case another person tries to use this solution, it is good to realize.
@david, I suggest you ask your second question as a brand-new one, rather than as a comment here. You will get more "traffic" seeing it.

请先登录,再进行评论。

更多回答(1 个)

Here is a slick one-liner:
arrayfun(@(x,y) (ceil(min([x y])):floor(max([x,y])))',A,B,'UniformOutput',false)
Here is a straightforward way with a loop:
A = [4.54 5.11 9.58 15.04];
B = [1.01 3.2 5.5 16.98];
N = length(A);
X = cell(1,length(A));
for i = 1:N
hi = floor(max([A(i) B(i)]));
lo = ceil(min([A(i) B(i)]));
X{i} = (lo:hi)';
end

类别

帮助中心File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by