Creating a n*2 array from a variable in workspace

2 ビュー (過去 30 日間)
deep Dhillon
deep Dhillon 2019 年 1 月 30 日
回答済み: deep Dhillon 2019 年 2 月 2 日
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 ???
  4 件のコメント
madhan ravi
madhan ravi 2019 年 1 月 30 日
for k=1:size(data,1)
somefunction(data(k,:)) %like this ?
end
deep Dhillon
deep Dhillon 2019 年 1 月 30 日
So i am using this function
evalin('base', 'centers')
This brings my variable 'centers' into the command window,, which has n*2 elements(please see the attached image)
What i wanna do is to use each of the values in that centers variable and use each of them to run an entire algorithm on those values. So essentially, with reference to the image.. i would run algorithm on 424.5621 212.4266 (which is like the pair x1,y1) and then algorithm on 551.959 376.5003 ... until algo. on 299.3605 509.6506 as such,, thus on each pair of the variable
variable.PNG

サインインしてコメントする。

採用された回答

Image Analyst
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
0001 Screenshot.png

その他の回答 (2 件)

Luna
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
  1 件のコメント
deep Dhillon
deep Dhillon 2019 年 2 月 1 日
編集済み: Image Analyst 2019 年 2 月 2 日
Hi Luna
Thanks a lot for the reply
I tried couple of different ways, but i am not able to figure out precisely, how shall i write my function exactly. So indeed what i want to do is that to use all the x,y pairs of the centers variable (on my workspace, which is an n*2 array) and run the following algorithm to create the bounding boxes at those x,y coordinates.
% Creating bounding box at any given x,y
figure, imshow(mush);
hold on
x=425; % center location X
y= 211; % center location y
plot(centers)
boxHalfWidth = 40; %
boxXStart = x-boxHalfWidth;
boxYStart = y-boxHalfWidth;
boxWidth = 2 * boxHalfWidth;
boxHeight = 2 * boxHalfWidth;
rectangle('Position',[boxXStart boxYStart boxWidth boxHeight])
Essentially what i wanna do is that i should be able to input my function with the x,y and in turns it executes the above algorithm for each x,y pair in my center variable

サインインしてコメントする。


deep Dhillon
deep Dhillon 2019 年 2 月 2 日
Image analyst
It works really well, onto my application.
Greatly appreciate you for the support

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by