Assigning Null / Multi-Dimensional Matrix

조회 수: 4 (최근 30일)
tinkyminky93
tinkyminky93 2022년 4월 7일
답변: Arif Hoq 2022년 4월 7일
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
KSSV
KSSV 2022년 4월 7일
Question is not clear. What you want to do in the loop?
tinkyminky93
tinkyminky93 2022년 4월 7일
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
Arif Hoq 2022년 4월 7일
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

추가 답변 (1개)

KSSV
KSSV 2022년 4월 7일
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
  댓글 수: 1
tinkyminky93
tinkyminky93 2022년 4월 7일
편집: tinkyminky93 2022년 4월 7일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by