extract a number N of equally spaced rows within a matrix
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Alberto Acri
am 22 Sep. 2023
Kommentiert: Alberto Acri
am 24 Sep. 2023
HI! I have a number N and a matrix with 100 rows and 2 columns.
How can I recover N rows of the matrix so that they are as equidistant from each other as possible?
For example:
- with N = 10 I will have 10 rows (row 1, 11, 21, 31,...)
- with N = 7 I will have 7 rows (row 1, 15, 29,...) In this case the steps are like 100/7 = 14.28 = 14.
a=1:1:100;
a=a';
b=0.1:0.1:10;
b=b';
M=[a,b]; % example matrix
N=10; % number of rows
step = height(M)/N;
step = 14; % round down
v = 1:step:height(M);
M_new = M(v,:);
0 Kommentare
Akzeptierte Antwort
Fangjun Jiang
am 22 Sep. 2023
Bearbeitet: Fangjun Jiang
am 22 Sep. 2023
M=repmat((1:100)',1,2);
N=7;
d=M(round(linspace(1,height(M),N)),:)
0 Kommentare
Weitere Antworten (1)
Dyuman Joshi
am 22 Sep. 2023
Bearbeitet: Dyuman Joshi
am 22 Sep. 2023
You are on the right course -
a=(1:1:100)';
b=(0.1:0.1:10)';
M=[a,b]; % example matrix
N=7; % number of rows
step = height(M)/N
%Use floor to round down to nearest integer less than or equal to step
step = floor(step) % round down
v = 1:step:height(M)
M_new = M(v,:)
8 Kommentare
Dyuman Joshi
am 24 Sep. 2023
Bearbeitet: Dyuman Joshi
am 24 Sep. 2023
"when height(M) is 100 and N is 10, it is hard to say which one is the right answer."
OP has specified what the expected outcome is and the logic behind it -
HI! I have a number N and a matrix with 100 rows and 2 columns.
How can I recover N rows of the matrix so that they are as equidistant from each other as possible?
For example:
- with N = 10 I will have 10 rows (row 1, 11, 21, 31,...)
- with N = 7 I will have 7 rows (row 1, 15, 29,...) In this case the steps are like 100/7 = 14.28 = 14.
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!