# Assigning Null / Multi-Dimensional Matrix

2 views (last 30 days)
tinkyminky93 on 7 Apr 2022
Answered: Arif Hoq on 7 Apr 2022
Hello,
I have a matrix with the dimensions of 4x2500 and I am generating this matrix in for loop. I want this matrix to be 4x1250. Without multi dimension I just write A(1:length(X)) = [] but I am stuck in multi dimension. This logic does not work in my operation.
when I say
A = [1:1:2500];
A (1:1250) = [];
it works and that is what i want to do.
but in multi dimension,
for i = 1:1:4
A(i,:) = ????????
end
Can someone help me? Thanks.
##### 2 CommentsShowHide 1 older comment
tinkyminky93 on 7 Apr 2022
I have an array and I am writing this array to every row of the matrix. For example lets say I have [1:1:5]. What I do in for loop is
A(i,:) = my array
i'th row : 1 2 3 4 5
i+1'th row: 1 2 3 4 5
i+2'th row: 1 2 3 4 5
i+3'th row: 1 2 3 4 5
I want to assign null to every row of this matrix, then it will become
i'th row : 4 5
i+1'th row: 4 5
i+2'th row: 4 5
i+3'th row: 4 5

Arif Hoq on 7 Apr 2022
vectorized solution is the most efficient and simple. But, still if you need for loop, try this
x=1:5;
A = repmat(x,4,1);
for i = 3:-1:1 % if you want to delete from Column 3 then index i will be started from 3.
A(:,i) = []
end

KSSV on 7 Apr 2022
A = repmat(1:5,4,1)
A = 4×5
1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
A(:,1:3) = []
A = 4×2
4 5 4 5 4 5 4 5
tinkyminky93 on 7 Apr 2022
Edited: tinkyminky93 on 7 Apr 2022
Can you try it with for loop? Will it be the same? I am getting error when i try
A = repmat(1:5,4,1);
for i = 1:1:4
A(:,1:3) = [];
end
>> Matrix index is out of range for deletion

### Categories

Find more on Matrix Indexing in Help Center and File Exchange

R2021b

### Community Treasure Hunt

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

Start Hunting!