pre allocating / extracting data from vector based on condition
Afficher commentaires plus anciens
לק"י
Hello!
I want to find a way other then if statement to go through vector and check if some conditions apply to each cell and to return 1 or 0 in accordance to the result of the conditions.
for example, I have a vector called vorroi, which contains the candidate points to be ploted in a plot.
the current process that decides if the current cell of vorroi will be plottted (passed the statements) is this for statement:
if ismember(1, c{j})==0 && (isempty(polyxpoly(crsvrtcl(:,1),crsvrtcl(:,2),v(c{j},1),v(c{j},2)))==0 || isempty(polyxpoly(crshrzntl(:,1),crshrzntl(:,2),v(c{j},1),v(c{j},2)))==0) %ismember(0, inpolygon(v(c{j},1),v(c{j},2), fhpos(:,1), fhpos(:,2)))~=1
which asks if the candidate point voronoi *(some background about voronoi in the end) area is inf (ismember(1, c{j})), and if it crosses one of two lines I draw (isempty statements).
It would be very beneficial to compute the result of the if statement without the need to fo through that loop. is it possible? and how?
I tried several attempts, such as:
plgnsinmrkd=[vorroi(ismember(1, c{vorroi(:,1)})==0 && (isempty(polyxpoly(crsvrtcl(:,1),crsvrtcl(:,2),v(c{vorroi(:,1)},1),v(c{vorroi(:,1)},2)))==0 || isempty(polyxpoly(crshrzntl(:,1),crshrzntl(:,2),v(c{vorroi(:,1)},1),v(c{vorroi(:,1)},2)))==0))];
plgnsinmrkd=vorroi(ismember(1, c{vorroi(:,1)})==0 && (isempty(polyxpoly(crsvrtcl(:,1),crsvrtcl(:,2),v(c{vorroi(:,1)},1),v(c{vorroi(:,1)},2)))==0 || isempty(polyxpoly(crshrzntl(:,1),crshrzntl(:,2),v(c{vorroi(:,1)},1),v(c{vorroi(:,1)},2)))==0));
plgnsinmrkd=(vorroi(ismember(1, c{vorroi(:,1)})==0 && (isempty(polyxpoly(crsvrtcl(:,1),crsvrtcl(:,2),v(c{vorroi(:,1)},1),v(c{vorroi(:,1)},2)))==0 || isempty(polyxpoly(crshrzntl(:,1),crshrzntl(:,2),v(c{vorroi(:,1)},1),v(c{vorroi(:,1)},2)))==0)));
and it didn't do it, the output is:
> plgnsinmrkd=(vorroi(ismember(1, c{vorroi(:,1)})==0 && (isempty(polyxpoly(crsvrtcl(:,1),crsvrtcl(:,2),v(c{vorroi(:,1)},1),v(c{vorroi(:,1)},2)))==0 || isempty(polyxpoly(crshrzntl(:,1),crshrzntl(:,2),v(c{vorroi(:,1)},1),v(c{vorroi(:,1)},2)))==0)));
>> plgnsinmrkd
plgnsinmrkd =
[]
I thiought it would be something like other line I wrote:
datintmrkd=(cell2mat(dat(:,3))>minint(m) & maxint(m)>cell2mat(dat(:,3))); %create a vector that assings 1 to coresponding cell within data only if in minint and maxint range.
but somewhy it doesn't work.
Sorry for all the questions lately, and thank you all!
Amit.
*A little background - Voronoin gives back 2 vectors, v and c that are the (x and y points of the vertices - v vector) and (index of vertices which relate to each polygon created in the voronoi - c vector). more info here: N-D Voronoi diagram - MATLAB voronoin (mathworks.com).
4 commentaires
Jan
le 9 Fév 2023
"is this for statement:" - There is no for statement in the posted code.
"which asks if the candidate point voronoi *(some background about voronoi in the end) area is inf (ismember(1, c{j})), and if it crosses one of two lines I draw (isempty statements)." - The ismember command is commented, such that the description does not match the code.
"It would be very beneficial to compute the result of the if statement without the need to fo through that loop." - Without seeing the relevant part of the code, especially the loop, it is hard to estimate, if another code is faster.
Due to the need of vertical scrolling lines as:
plgnsinmrkd=[vorroi(ismember(1, c{vorroi(:,1)})==0 && (isempty(polyxpoly(crsvrtcl(:,1),crsvrtcl(:,2),v(c{vorroi(:,1)},1),v(c{vorroi(:,1)},2)))==0 || isempty(polyxpoly(crshrzntl(:,1),crshrzntl(:,2),v(c{vorroi(:,1)},1),v(c{vorroi(:,1)},2)))==0))];
are extremely hard to read and to understand. Simplifying the names of the variables would be very useful also:
A = vorroi(:, 1);
B = crsvrtcl;
C = crshrzntl;
y = A(ismember(1, c{A}) == 0 && ...
(~isempty(polyxpoly(B(:,1), B(:,2), v(B{A}, 1), v(c{A},2))) || ...
~isempty(polyxpoly(C(:,1), C(:,2), v(c{A}, 1), v(c{A},2)))));
The next simplification is:
ismember(1, c{A(:,1)}) == 0 % ==>
all(c{A(:,1)} ~= 1)
This does not solve your problem, but I can read it successfully at least.
Amit Ifrach
le 9 Fév 2023
Modifié(e) : Amit Ifrach
le 9 Fév 2023
Jan
le 10 Fév 2023
Sorry, this is far too extensive for a question in the forum. Split the problem into parts. If a question can be understood in less then 2 minutes, it has much higher chances to get an answer. Maybe this helps: https://www.mathworks.com/matlabcentral/answers/6200-tutorial-how-to-ask-a-question-on-answers-and-get-a-fast-answer .
Amit Ifrach
le 11 Fév 2023
Réponses (0)
Catégories
En savoir plus sur Voronoi Diagram dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

