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 Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by