Creating a n*2 array from a variable in workspace
조회 수: 2 (최근 30일)
이전 댓글 표시
I have a variable on my workspace, in which i have n*2 elements ( 'n' rows and 2 columns). I wanna create an array to do some computation on all the elemental pairs of that variable, such that x1,y1... How can i do that ???
채택된 답변
Image Analyst
2019년 2월 2일
Try this and then adapt as needed:
% Creating bounding box at any given x,y
grayImage = imread('moon.tif');
[rows, columns, numberOfColorChannels] = size(grayImage)
imshow(grayImage);
boxHalfWidth = 40; %
xy = boxHalfWidth + rand(10, 2) * (rows - 2 * boxHalfWidth);
for k = 1 : size(xy, 1)
x = xy(k, 1);
y = xy(k, 2);
hold on
plot(x, y, 'r+', 'MarkerSize', 25)
boxXStart = x-boxHalfWidth;
boxYStart = y-boxHalfWidth;
boxWidth = 2 * boxHalfWidth;
boxHeight = 2 * boxHalfWidth;
rectangle('Position',[boxXStart boxYStart boxWidth boxHeight], 'EdgeColor', 'r')
end
댓글 수: 0
추가 답변 (2개)
Luna
2019년 1월 30일
편집: Luna
2019년 1월 30일
Please read this below:
eval and evalin are not recommended. Why you are not just creating a function that gets your nx2 array as the input variable? And a for loop that gets your array's each row one by one?
For example:
function myOutputs = myAlgorithm(centers)
c = [];
for i = 1:size(centers,1)
x = centers(i,1);
y = centers(i,2);
% do what you want with your x and y and then next iteration x and y will be your next row of your array
c = [c x*y]; % just an example
end
myOutputs = c % just an example
end
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!