How to select specific matrix rows trough specifik given numbers

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 commentaires

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.
vimal - please put down below in the Answer section with the rest of the answers. Thanks in advance.

Connectez-vous pour commenter.

 Réponse acceptée

Try this:
% Determine which rows to extract:
rowsToSelect = A(:, 4) == 15;
% Make a new matrix with only those rows extracted:
extractedRows = A(rowsToSelect, :);

3 commentaires

Could this action be automated, say i have 'n' rows with 24 different values i want to apply this command to but without having to write the same command 24 times?
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, :);
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

Connectez-vous pour commenter.

Plus de réponses (1)

If A is your N x 6 matrix and B is your N x 4 matrix,
B(A(:,4)==15,:)

Catégories

Produits

Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by