How to select specific matrix rows trough specifik given numbers
18 views (last 30 days)
Show older comments
Joakim Mørk
on 22 Apr 2020
Commented: Image Analyst
on 29 Apr 2020
Hey guys. Im working on a project where i have i to pick out specific rows in one N x 6 matrix matching a specifik given value.
Then using the placement of those specific rows in order to calculate data within those specific rows in a different N x 4 matrix.
The rows within the N x 6 matrix is combined like: [ year , month , day , hour , minute , second ]
So for an example i need to find all rows with the hour 15, i find two rows in that specific dataset one in row 4 and one in 10. I then need to calculate the exact same rows within the N x 4 matrix.
Any ideas to how this is done?
i've tried out with A(: , 4==15) but that doesn't tell the sepcific rownumber.
2 Comments
vimal kumar chawda
on 22 Apr 2020
First mention the number of loop with some i and after that mention at which number of loop the requirement fulfill. And if the loop number is 5 then it should stored the value at position 5th. all other matrix will be 0 and the you will get row number.
Or you can mention with another variable with k so if i all requirement fulfill then stores the x value which is row (x,y). you have to define the iteration perfectly.
Image Analyst
on 22 Apr 2020
vimal - please put down below in the Answer section with the rest of the answers. Thanks in advance.
Accepted Answer
Image Analyst
on 22 Apr 2020
Try this:
% Determine which rows to extract:
rowsToSelect = A(:, 4) == 15;
% Make a new matrix with only those rows extracted:
extractedRows = A(rowsToSelect, :);
3 Comments
Image Analyst
on 29 Apr 2020
You should put all your n in some vector, like
n = [15,123,4234,22,3423, etc for all your 24 values]
Then you'd do something like this:
for k = 1 : length(n)
% Get the k'th value of n
this_n = n(k);
% Determine which rows to extract:
rowsToSelect = A(:, 4) == this_n;
% Make a new matrix with only those rows extracted:
extractedRows = A(rowsToSelect, :);
% Now process extractedRows in whatever way you want to...
end
If your n are sequential, you can do
n = 1 : 24;
then the for loop I had above, or else you can do
for k = 1 : n
% Determine which rows to extract:
rowsToSelect = A(:, 4) == n;
% Make a new matrix with only those rows extracted:
extractedRows = A(rowsToSelect, :);
% Now process extractedRows in whatever way you want to...
end
More Answers (1)
Tommy
on 22 Apr 2020
If A is your N x 6 matrix and B is your N x 4 matrix,
B(A(:,4)==15,:)
0 Comments
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!