Removing rows from array using indices
显示 更早的评论
I have a 2530x1 array named Rowidx containing the indices of rows that I want to remove from several other arrays that have different dimenions.
What's the best way of doing this in MATLAB?
采纳的回答
If ‘Rowidx’ is a logical array, that might not work as desired. If it is a numerical array, then if ‘A’ is any of the arrays —
A(Rowidx,:) = [];
would work.
.
10 个评论
Pelajar UM
2021-11-1
编辑:Pelajar UM
2021-11-1
Thanks, this works but what if I want to create a new array B instead of modifying the existing array A?
As always, my pleasure!
One option would be to first create ‘B’ as a copy of ‘A’ —
A = randi(9, 7, 3)
A = 7×3
8 6 8
3 6 3
8 6 9
7 6 2
2 7 4
1 5 8
8 9 3
Rowidx = [2; 5];
B = A;
B(Rowidx,:) = []
B = 5×3
8 6 8
8 6 9
7 6 2
1 5 8
8 9 3
A different option, using ‘logical indexing’ would be —
Lidx = true(size(A,1),1);
Lidx(Rowidx) = false
Lidx = 7×1 logical array
1
0
1
1
0
1
1
B = A(Lidx,:)
B = 5×3
8 6 8
8 6 9
7 6 2
1 5 8
8 9 3
I am not certain what ‘different dimensions’ means here.
If all the arrays have the same row sizes with different numbers of columns, the advantage to the ‘logical indexing’ approach is that ‘Lidx’ could be used with every subsequent array without first having to copy the output arrays.
However if the row sizes are different, the first approach would be best.
.
Pelajar UM
2021-11-1
编辑:Pelajar UM
2021-11-1
Great! Thank you so much. This is very helpful. I managed to clean up the indices with this but now I have a different problem. :(
The idx is 8916x1 and the value of indices inside are between 1 and 15. I want to apply it to a 8916x45 array.
45 actually represents a collection of 15 x,y,z coordinates (15x3). So when I apply the idx, it is supposed to choose a collection of 3 points rather than just one number and the output would be 8916x3. How can I approach this?
As always, my pleasure!
If the indices are numbers between 1 and 15, it is only possible to address rows within that range. I am having problems visualising the contents of the (8916x45) array. It would seem that the array is constructed something like
[x_1 y_1 z_1 x_2 y_2 z_2 ... x_15 y_15_z_15]
for each row, and the objective is to choose one of those sets.
perhaps something like
Rowidx = randi(15, 5,1)
Rowidx = 5×1
10
14
15
1
4
Select = (Rowidx-1)*3+(1:3)
Select = 5×3
28 29 30
40 41 42
43 44 45
1 2 3
10 11 12
would work. The ‘Select’ variable are then the column indices for each row.
.
I tried to follow your code, but this gives a 8961x26748 double for some reason:
Select = (anglesidx-1)*3+(1:3) % the ouput looks Ok to me.
SortedCentroid3 = sortedCentroidmat (:,Select); % here seems to be where I'm making a mistake.
In my code, ‘Select’ would be the columns to retain, because it is not possible to arbitrarily remove columns from an array and maintain the array architecture. Those columns would have to be copied to a new (Nx3) matrix.
So it would be necessary to use set arithmetic (specifically setdiff, or for a logical vector, logical operations to select the rows) on the row index of the entire array to retain only the desired rows in the new matrix. The new matrix would be less than the original matrix by whatever the desired number is.
The ‘anglesidx’ array would appear to be the original ‘Rowidx’ and the chosen columns would be ‘Select’ in my code. As I understand the problem that this has transformed into, for all this to work correctly, ‘anglesidx’ would have to have one entry (on the interval (1,15)) for each row of whatever the target matrix is. If not all rows would be considered, ‘anglesidx’ would itself have to be indexed by a logical array that selects the rows to be imported to the new matrix.
.
Pelajar UM
2021-11-1
编辑:Pelajar UM
2021-11-1
I have a bit of difficulty understanding why the last line doesn't work.
This is how "Select" looks like:

Doesn't this mean that in row 1, take columns 13,14,15 and in row 2, take columns 25,26,27 and so on?
Then why the output doesn't have the same dimensions (8916x3)? Where does 26748 come from?
‘Doesn't this mean that in row 1, take columns 13,14,15 and in row 2, take columns 25,26,27 and so on?’
Yes.
‘Then why the output doesn't have the same dimensions (8916x3)?’
It should. It turns out that the sort of vectorised indexing is not appropriate here.
A simple for loop solves that problem —
A = randi(9, 5, 45)
A = 5×45
7 3 5 9 9 6 2 8 4 3 5 6 3 3 8 9 1 8 9 5 8 7 9 9 8 2 1 8 9 9
2 8 3 2 9 6 8 5 4 1 1 3 1 7 6 5 8 4 6 9 7 8 5 5 5 9 6 2 7 2
3 5 7 3 2 2 9 7 1 3 6 1 3 7 4 4 6 8 6 6 3 2 7 9 6 9 6 6 4 1
8 1 6 7 8 8 9 9 2 2 8 3 3 7 4 4 9 7 3 5 7 8 1 4 9 4 4 4 5 1
5 8 1 7 4 3 9 8 9 4 4 9 8 3 6 5 2 6 2 2 1 5 4 8 2 5 2 5 1 3
Rowidx = randi(15, 5,1)
Rowidx = 5×1
7
15
4
10
2
Select = (Rowidx-1)*3+(1:3)
Select = 5×3
19 20 21
43 44 45
10 11 12
28 29 30
4 5 6
for k = 1:size(A,1)
B(k,:) = A(k,Select(k,:));
end
B
B = 5×3
9 5 8
9 3 1
3 6 1
4 5 1
7 4 3
‘Where does 26748 come from?’
The vectorised indexing approach will not produce the correct result. It is necessary to use a for loop to get it to work as I intend it to work.
.
Wonderful! Thanks a lot for your time. I really appreciate it.
Thank you!
As always, my pleasure!
.
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
