Upsampling a matrix with zero elements

12 ビュー (過去 30 日間)
Alí Fernando Vela Pérez
Alí Fernando Vela Pérez 2024 年 1 月 31 日
コメント済み: Stephen23 2024 年 1 月 31 日
I have a matrix x = [1 2 3 4; 5 6 7 8] and I want to upsample it by 2 so it goes something like x = [1 0 2 0 3 0 4 0; 0 0 0 0 0 0 0 0; 5 0 6 0 7 0 8 0; 0 0 0 0 0 0 0 0]
Any suggestion??

回答 (3 件)

Dyuman Joshi
Dyuman Joshi 2024 年 1 月 31 日
編集済み: Dyuman Joshi 2024 年 1 月 31 日
%input
x = [1 2 3 4; 5 6 7 8]
x = 2×4
1 2 3 4 5 6 7 8
%Upsample factor
n=2;
%preallocate the output matrix
y = zeros(n*size(x));
%assign values
y(1:n:end,1:n:end) = x
y = 4×8
1 0 2 0 3 0 4 0 0 0 0 0 0 0 0 0 5 0 6 0 7 0 8 0 0 0 0 0 0 0 0 0
  1 件のコメント
Stephen23
Stephen23 2024 年 1 月 31 日
+1 good MATLAB approach.

サインインしてコメントする。


Bruno Luong
Bruno Luong 2024 年 1 月 31 日
x = [1 2 3 4; 5 6 7 8]
x = 2×4
1 2 3 4 5 6 7 8
kron(x, [1 0; 0 0])
ans = 4×8
1 0 2 0 3 0 4 0 0 0 0 0 0 0 0 0 5 0 6 0 7 0 8 0 0 0 0 0 0 0 0 0
  1 件のコメント
Stephen23
Stephen23 2024 年 1 月 31 日
+1 very neat

サインインしてコメントする。


Anjaneyulu Bairi
Anjaneyulu Bairi 2024 年 1 月 31 日
編集済み: Anjaneyulu Bairi 2024 年 1 月 31 日
Hi,
You can follow the below steps to upsample the matrix by 2.
  • Create a matrix with a double number of rows and columns with all zeros.
  • Now iterate over this new matrix, and for every odd iteration and odd position, assign the original matrix value.
Code for reference
x = [1 2 3 4;5 6 7 8];
[rows, cols] = size(x);
% Initialize the matrix with double number of rows and columns
new_matrix = zeros(rows * 2, cols * 2);
% Copy elements from the original matrix 'x' to the new matrix
for i = 1:rows
for j = 1:cols
new_matrix(2*i-1, 2*j-1) = x(i, j);
end
end
Hope it helps to resolve your query.

カテゴリ

Help Center および File ExchangeImages についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by