MATLAB Answers

Access Matrix using array as keys

1 view (last 30 days)
James kivai
James kivai on 25 Jun 2021
Commented: VBBV on 26 Jun 2021
I have a matrix
allPoints = ["10999970X10999700Y" "11000310X12099960Y" "10999970X12999700Y";
"11999970X10999960Y" "11999970X12000230Y" "11999970X13000230Y";
"12999970X10999700Y" "12999970X12000230Y" "12999970X13000230Y"]
And I want to select points based of rectangles, where I have bottom and top points of the rectangle
For a single rectangle with
bottom = [1 1]; top = [2 2];
I would use:
coveredPoints = allPoints(bottom(1):top(1),bottom(2):top(2))
%% OUTPUT
% coveredPoints =
%
% 2×2 string array
%
% "10999970X10999700Y" "11000310X12099960Y"
% "11999970X10999960Y" "11999970X12000230Y"
which would return all the points from (1,1) to (2,2).
But now I have multiple bottoms and tops, and I want to get all the covered areas into a n x 1 array (single column array)
bottoms = [1 3; 2 2]; tops=[1 3; 2 2];
% When I try
coveredPoints = allPoints(bottoms(:,1):tops(:,1), bottoms(:,2):tops(:,2));
% It only returns the values for the first row only
% coveredPoints =
%
% "10999970X12999700Y"
Is there a way to extract all covered points onto a n x 1 array without a loop?

Answers (2)

VBBV
VBBV on 26 Jun 2021
%if true
coveredPoints = allPoints(bottoms(:,1):tops(:,2), bottoms(:,1):tops(:,1))
Try this
  1 Comment
VBBV
VBBV on 26 Jun 2021
If you want to extract whole of array
%if true
coveredPoints = allPoints(bottoms(:,1):tops(:,2), bottoms(:,1):tops(:,2))

Sign in to comment.


Community Treasure Hunt

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

Start Hunting!

Translated by