Vectorization with two dimensions at the same time

I have 2x10x10 matrix.
ans(:,:,1) =
1 3 5 7 9 11 13 15 17 19
2 4 6 8 10 12 14 16 18 20
ans(:,:,2) =
21 23 25 27 29 31 33 35 37 39
22 24 26 28 30 32 34 36 38 40
...
a(:,:,9) =
161 163 165 167 169 171 173 175 177 179
162 164 166 168 170 172 174 176 178 180
a(:,:,10) =
181 183 185 187 189 191 193 195 197 199
182 184 186 188 190 192 194 196 198 200
I want to erase the certain column of each 10 2D matries.
The rule is simple, on case of the first 2D matrix, erase first column. And second matrix, erase second column. Proceed diagonally.
If i implement this idea into for loop, it would be
for i=1:10
a(:,i,i)= [];
end
As I checked, it doesn't work. MATLAB produces the error saying "a null assignment can have only one non-colon index".
If above code works, then result should be like 2x9x10 matrix.
ans(:,:,1) =
3 5 7 9 11 13 15 17 19
4 6 8 10 12 14 16 18 20
ans(:,:,2) =
21 25 27 29 31 33 35 37 39
22 26 28 30 32 34 36 38 40
...
a(:,:,9) =
161 163 165 167 169 171 173 175 179
162 164 166 168 170 172 174 176 180
a(:,:,10) =
181 183 185 187 189 191 193 195 197
182 184 186 188 190 192 194 196 198
After writing, It looks like there are two questions about it.
  1. How can I assign null array by using two indices (`a(:,i,i)`)?
  2. And how can I vectorize it?
In vectorization, I also tried the pythonic way, but it didn't work.
a(:, 1:10, 1:10) = []; % a null assignment can have only one non-colon index

 채택된 답변

Matt J
Matt J 2019년 12월 17일
편집: Matt J 2019년 12월 17일
How can I assign null array by using two indices (`a(:,i,i)`)?
You cannot. The way you would get what you're after here is as follows.
[m,n,p]=size(a);
a(:,1:n+1:end)=[];
a=reshape(a,m,[],p)

추가 답변 (1개)

Matt J
Matt J 2019년 12월 17일
Here's a less efficient way, but one which generalizes more flexibly to other kinds of deletions you might want to do,
[m,n,p]=size(a);
[~,J,K]=ndgrid(1:m,1:n,1:p);
a(J==K)=[];
a=reshape(a,m,[],p);

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2019년 12월 17일

댓글:

2019년 12월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by