How to select specific matrix rows trough specifik given numbers

18 views (last 30 days)
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
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
Image Analyst on 22 Apr 2020
vimal - please put down below in the Answer section with the rest of the answers. Thanks in advance.

Sign in to comment.

Accepted Answer

Image Analyst
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
Joakim Mørk
Joakim Mørk on 29 Apr 2020
And could you do the same command but without knowing the valubel to set equal?
% Extracting rows of value 'n':
rowsToSelect = A(:, 4) == n;
% Make a new matrix with only those rows extracted:
extractedRows = A(rowsToSelect, :);
Image Analyst
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

Sign in to comment.

More Answers (1)

Tommy
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,:)

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!