You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
how to use logic array
28 views (last 30 days)
Show older comments
i have a 8x6 logical array
result1 =
8×6 logical array
1 0 0 0 0 0
0 0 0 1 0 0
0 0 0 0 1 0
0 0 0 0 0 0
0 1 0 0 0 0
0 0 1 0 0 0
0 0 0 0 0 1
0 0 0 0 0 0
how to use this logical array to extract the values in two matrix assosiated with logical ones
8 Comments
madhan ravi
on 1 Aug 2019
You need to be clear how does your look like? the sizes??
karishma koshy
on 1 Aug 2019
these are the two arrays:
329.8325 361.1270
613.6074 215.8294
376.3128 310.3919
636.6237 446.4107
111.5548 191.3145
248.6901 372.9032
338.8193 3.1024
428.1734 211.9613
row_frame2_adata =
329.7856 364.9094
111.9948 187.5294
253.1863 368.9889
614.6203 217.2036
378.9097 307.9001
259.9949 27.1064
the elements that nearly similar are linked and gives output as one. how can I use to find the elements linked . That is 1 is second column shows that 111.5548 191.3145 in frame 1 is linked to 111.9948 187.5294 in frame 2.
but i want to use the one in logical array to find all the elements linked similarly as mentioned above.
karishma koshy
on 1 Aug 2019
Edited: karishma koshy
on 1 Aug 2019
I want to find the values associated with the logical ones
KALYAN ACHARJYA
on 1 Aug 2019
Edited: KALYAN ACHARJYA
on 1 Aug 2019
Madhan, already ask about sizes? Have you looked on it? I did not get any clue, just do combine of frame1 and frame2, give us the results with 8x6, rest is easy.
If you want to any logical operation between result1 and frame, both must be the same.
Or
Elaborate the question with simple example?
karishma koshy
on 1 Aug 2019
I am trying to locate the values that linked to the logical ones. So that I can identify it's movement and add the ID number later on.
So all I want is a for loop where I can take the size of row_frame1_data array, use the assignment matrix that i get from the Munkres algorithm to find out which points in adjacent frames have been linked.
Guillaume
on 1 Aug 2019
@karishma, you asked this question already and I asked for clarification. Instead of clarifying you deleted that question and reposted it with even less information.... and you get asked the same clarifications.
So,if you want help, rather than wasting everybody's time do answer the questions that you're asked and spend time explaining everything clearly.
The question that you deleted was a lot better expressed than what you've written above even though it wasn't clear. It's even less clear now.
Personally, I won't contribute anymore since my input wasn't thought worthwile.
karishma koshy
on 1 Aug 2019
Hi Sir
I thought it was not clear and had less information. That why I resubmitted it added informations. Sorry for your inconvenience.
Accepted Answer
Jon
on 1 Aug 2019
It isn't quite clear the specifics of what you want to retrieve but for example if you wanted to get the row in frame 1 that matches row 2 in frame 2 you could use
row = row_frame1_adata(result1(:,2),:)
17 Comments
karishma koshy
on 1 Aug 2019
Edited: karishma koshy
on 1 Aug 2019
Hi Sir,
Yes, I want to retrieve values of frame 1 and frame 2 values associated with all logical ones. So that I can add ID number to those that are similar.
So all I want is a for loop where I can take the size of row_frame1_data array, use the assignment matrix that i get from the Munkres algorithm to find out which points in adjacent frames have been linked
Jon
on 1 Aug 2019
Edited: Jon
on 1 Aug 2019
Sorry, I'm still struggling to understand what exactly you are trying to do. I am thinking though that you have some ID's for the rows in frame1, say they are just the row numbers, 1,2,...8
Then the matching ID's for each row in frame2 would be given by
[id2,~] = find(result1)
If you have one more level of indirection, and your ids for frame 1 are given by a vector, call it id1, then you could get the ids in frame 2 using
[idx,~] = find(result1)
id2 = id1(idx)
Note it is important to use two left hand arguments (as I did above) when calling find so that it returns row and column indices rather than linear indices.
If I am still not understanding what you are trying to do, then please make a simple example which gives the inputs, and desired outputs that you are looking for.
karishma koshy
on 1 Aug 2019
row_frame1_data =
999.0000 329.8325 361.1270 22.0027
999.0000 613.6074 215.8294 28.1989
999.0000 376.3128 310.3919 22.9178
999.0000 636.6237 446.4107 27.1672
999.0000 111.5548 191.3145 24.8943
999.0000 248.6901 372.9032 41.9425
999.0000 338.8193 3.1024 23.3755
999.0000 428.1734 211.9613 23.5272
row_frame2_data =
1.0e+03 *
1.0000 0.3298 0.3649 0.0227
1.0000 0.1120 0.1875 0.0236
1.0000 0.2532 0.3690 0.0371
1.0000 0.6146 0.2172 0.0307
1.0000 0.3789 0.3079 0.0216
1.0000 0.2600 0.0271 0.0244
FRAME CENTRE X CENTRE Y RADIUS ID
999.0000 329.8325 361.1270 22.0027 1
999.0000 613.6074 215.8294 28.1989 4
999.0000 376.3128 310.3919 22.9178 5
999.0000 636.6237 446.4107 27.1672
999.0000 111.5548 191.3145 24.8943 2
999.0000 248.6901 372.9032 41.9425 3
999.0000 338.8193 3.1024 23.3755 6
999.0000 428.1734 211.9613 23.5272
1.0e+03 *
1.0000 0.3298 0.3649 0.0227 1
1.0000 0.1120 0.1875 0.0236 2
1.0000 0.2532 0.3690 0.0371 3
1.0000 0.6146 0.2172 0.0307 4
1.0000 0.3789 0.3079 0.0216 5
1.0000 0.2600 0.0271 0.0244 6
karishma koshy
on 1 Aug 2019
Edited: karishma koshy
on 1 Aug 2019
My ultimate aim is to assign ID so i can track the moving object. Same as we label number in survillance camera to track the person's movement. here im trying to track multiple circles.
Jon
on 1 Aug 2019
Edited: Jon
on 1 Aug 2019
Here is a script which produces the results you show in your example
% example input arrays
row_frame1_data = [
999.0000 329.8325 361.1270 22.0027
999.0000 613.6074 215.8294 28.1989
999.0000 376.3128 310.3919 22.9178
999.0000 636.6237 446.4107 27.1672
999.0000 111.5548 191.3145 24.8943
999.0000 248.6901 372.9032 41.9425
999.0000 338.8193 3.1024 23.3755
999.0000 428.1734 211.9613 23.5272];
row_frame2_data = 1e3*[
1.0000 0.3298 0.3649 0.0227
1.0000 0.1120 0.1875 0.0236
1.0000 0.2532 0.3690 0.0371
1.0000 0.6146 0.2172 0.0307
1.0000 0.3789 0.3079 0.0216
1.0000 0.2600 0.0271 0.0244];
result1 = logical(...
[1 0 0 0 0 0
0 0 0 1 0 0
0 0 0 0 1 0
0 0 0 0 0 0
0 1 0 0 0 0
0 0 1 0 0 0
0 0 0 0 0 1
0 0 0 0 0 0]);
% make arrays into tables
data1 = array2table(row_frame1_data,'VariableNames',{'frame','centre_x','centre_y','radius'});
data2 = array2table(row_frame2_data,'VariableNames',{'frame','centre_x','centre_y','radius'});
% assign id's to data2
data2.id = (1:size(data2,1))';
% find row numbers in frame1 data that have matching rows in frame2 data
% (these are the rows of result1 that have a 1 in them)
iRow = find(any(result1,2));
% find row numbers in frame 2 where each match occurs
[id,~] = find(result1');
% assign these ids in data1
% rows that don't have a matching ID will be assigned NaN
data1.id = NaN(size(data1,1),1);
data1.id(iRow) = id;
% display results
disp(data1)
disp(data2)
karishma koshy
on 1 Aug 2019
Dear Sir
Thank you so much for your time and consideration sir,
That's exactly what I wanted.
karishma koshy
on 2 Aug 2019
Edited: karishma koshy
on 2 Aug 2019
Hi Sir, I have actually 1000 frames like this where I need ID number satisfying the above condition. Right now I'm getting only last two frames if I doing it in a loop.
I need ID number 1 for all the 1000 frames similar to what you have done.
How can I do that.
Jon
on 2 Aug 2019
In each loop iteration you will have to assign your result to some variable (array, structure, table) that is indexed by the loop count. So, for example you could make an array of structures, let's call it S, with fields, frame, centre_x, centre_y, radius, id. At the end of each loop iteration you would then assign the results with something like:
% preallocate array of structures
numFrames = 1000
S(numFrames) = struct('frame',[],'centre_x',[],'centre_y',[],'radius',[],'id'[])
for iFrame = 1:numFrames
% body of loop where you do all of the work
% ...
% save results in output structure
S(iFrame).frame = data1.frame;
S(iFrame).centre_x = data1.centre_x;
S(iFrame).centre_y = data1.centre_y;
S(iFrame).radius = data1.radius;
S(iFrame).id = data1.id;
end
Note, that if you are doing all of the work inside of the loop, and all you care about is the final output structure, S, it is not really necessary to be creating those intermediate tables. I just used those in the example because it organized things well ,and gave a nicely formatted output with column headings. So if efficiency is a concern, you could rework the piece I gave you to do the same calculations but not bothering to put the values into the temporary tables. Alternatively, rather than using an array of structures, S, you could accumulate all of your results into a large table with an additional column indicating the frame count. You will have to work out the details for what works best for you.
karishma koshy
on 2 Aug 2019
Edited: karishma koshy
on 2 Aug 2019
Hi Sir
i am new to matlab. Can I know how to accumulate all of my results into a large table with an additional column indicating the frame count.
ans =
12×12 logical array
1 0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 1 0
0 0 0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 1 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 1 0 0 0 0 0
ans =
12×10 logical array
0 0 0 0 1 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0
1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 1 0 0 0
frame centre_x centre_y radius id
_____ ________ ________ ______ __
1 479.1 90.612 21.803 1
1 653.78 337.1 25.102 2
1 130.59 134.18 27.875 3
1 297.99 323.38 39.806 4
1 31.044 444.48 24.755 5
1 350.96 53.437 43.816 6
1 64.647 153.47 28.745 7
1 293.48 169.92 36.579 8
1 499.11 389.5 23.527 9
1 426.5 328.29 22.962 10
1 504.9 341.25 22.605 11
1 620.52 413.07 28.185 12
frame centre_x centre_y radius id
_____ ________ ________ ______ __
2 477.56 91.105 21.183 1
2 64.259 150.29 27.273 7
2 656.85 336.45 25.55 2
2 297.95 324.68 40.698 4
2 290.52 169.55 36.328 8
2 344.63 48.536 43.103 6
2 545.61 55.824 21.631 12
2 354.06 380.63 25.841 11
2 220.45 517.57 22.873 9
2 139.3 281.79 27.241 5
2 128.34 125.05 37.728 3
2 244.67 359.59 28.057 10
frame centre_x centre_y radius id
_____ ________ ________ ______ __
3 545.37 58.505 23.106 12
3 657.4 334.62 26.254 2
3 64.807 146.06 27.312 7
3 290.86 167.79 35.758 8
3 477.65 89.518 21.65 1
3 344.11 44.368 44.039 6
3 621.32 408.27 24.244 13
3 310.51 329.82 21.516 10
3 72.627 113.72 23.659 3
3 520.64 404.6 22.33 14
how can i get the above resultie the long table so on till 1000 frame
Jon
on 2 Aug 2019
You could do something like:
% preallocate cell array of tables to collect output
numFrames = 1000
data = cell(numFrames,1)
for iFrame = 1:numFrames
% body of loop where you do all of the work
% ...
% save results in output table
data{iFrame} = data1;
end
% combine all of the individual tables into one large table
data = vertcat(data{:})
karishma koshy
on 2 Aug 2019
In the body of loop can I use the previously sent code by you to assign ID number
Jon
on 2 Aug 2019
Edited: Jon
on 2 Aug 2019
I'm not completely clear on the details of your calculation but I think you should be able to adapt the code I sent you so that it can be included in the main loop. Since you seem to be doing a mapping of ids from the n+1 frame to the nth frame you probably have to be a little careful on the total loop count. Maybe it should only go to numFrames-1 so that you don't try to calculate beyond the end of your data
karishma koshy
on 2 Aug 2019
Is there a way where I can do this way - for 1:size(number of points in frame i+1) -find assigned points in frame i -if no assigned points, assign the next available ID - if there is an assigned point, set ID for point in I+1 equal to ID of point in I+1
Jon
on 2 Aug 2019
It sounds like you have a fairly clear understanding of what you want your code to do. You just need to translate your written description into MATLAB code that implements it. Just think carefully about each step you describe and I think you will be able to come up with the detailed MATLAB implementation. If you encounter some behavior that you really can't understand, or are stuck at some very specific point then ask for more help, but I think at this point you should be able to work out the details.
karishma koshy
on 2 Aug 2019
Dear Sir,
I know what to do theoretically, but I'm new to coding.
That's why I am stuck with this.
Jon
on 2 Aug 2019
Your application is quite complicated and probably is not a good place to start learning how to code. I would suggest that if you have not done so already you should first complete the free MATLAB On Ramp training,
Make sure you work your way all the way through this and don't skip any steps.
After you complete the training and come back to your problem I think you will find you will be able to code a solution.
More Answers (1)
Jackson Burns
on 1 Aug 2019
row_frame_data(result1)
Where row_frame_data is the 8*6 atrix of results and result1 is the logical matrix of the values you want.
4 Comments
karishma koshy
on 1 Aug 2019
Edited: karishma koshy
on 1 Aug 2019
When I tried this I got an error saying the logical indices contain a true value outside the array bounds
Mark Hayworth
on 1 Aug 2019
Exactly. Why is your logical matrix not the same size as the matrix you want to extract the values from. Your logical matrix has 4 more columns than your data. Why???
karishma koshy
on 1 Aug 2019
Edited: karishma koshy
on 1 Aug 2019
In horizontal it's the number of elements in frame k and in vertical it's the number of elements in frame k+1 ie it's 8x6 array. The logical array was obtained using Hungarian algorithm
Jackson Burns
on 1 Aug 2019
You will need to adjust the dimensions of your logical array to match the data.
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)