How to make certain indexes of a cell array empty?

32 ビュー (過去 30 日間)
Ethan
Ethan 2022 年 11 月 30 日
コメント済み: Voss 2022 年 11 月 30 日
I'm currently trying to write an image compression algorithm (I know this already exists in Matlab but this is for a course), and wondering if Matlab has functionality to input certain elements of a cell array as "empty". In Python and C# what I would do is set it to None/null, but I found that Matlab has no equivalent. Here is my current code where I would like to implement this:
if (y + by > imgHeight) || (x + bx > imgWidth)
pixelBlock{by + 1, bx + 1} = [-1, -1, -1]; % empty values here instead of -1
else
R = img(x + bx, y + by, 1);
G = img(x + bx, y + by, 2);
B = img(x + bx, y + by, 3);
pixelBlock{by + 1, bx + 1} = [R, G, B];
end
I'm blocking the image into 4x4 squares, but if a 4x4 square runs out of the bounds of the image I would like to put the pixel values as some sort of empty, where it can later be decoded and the program can ask (if pixel empty, skip).
Is this possible?

採用された回答

Voss
Voss 2022 年 11 月 30 日
pixelBlock{by + 1, bx + 1} = [];
  1 件のコメント
Voss
Voss 2022 年 11 月 30 日
or, equivalently:
pixelBlock(by + 1, bx + 1) = {[]};
But NOT:
pixelBlock(by + 1, bx + 1) = [];
because that attempts to remove the element pixelBlock(by + 1, bx + 1).
Example:
C = {1 2; 3 4} % 2-by-2 cell array
C = 2×2 cell array
{[1]} {[2]} {[3]} {[4]}
% set element 1,2 to the empty array, using {} indexing
C{1,2} = []
C = 2×2 cell array
{[1]} {0×0 double} {[3]} {[ 4]}
% set element 2,1 to the empty array, using () indexing
C(2,1) = {[]}
C = 2×2 cell array
{[ 1]} {0×0 double} {0×0 double} {[ 4]}
Another example (of the wrong thing, in this case):
C = {1 2 3 4} % 1-by-4 cell array
C = 1×4 cell array
{[1]} {[2]} {[3]} {[4]}
% remove element 1 (not what you want)
C(1) = []
C = 1×3 cell array
{[2]} {[3]} {[4]}

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

その他の回答 (1 件)

the cyclist
the cyclist 2022 年 11 月 30 日
Here are a couple ways:
% Make up a cell array of pixelBlock
pixelBlock = {'2','3','5',7; 11, 13, '17', '19'}
% Replace a few cells with empty
pixelBlock(1,2:4) = cell(1,3)
% Replace a couple more, in a different way
pixelBlock(2,1:2) = {[],[]}
Note that one needs to be careful about when one uses parentheses vs. curly brackets.

カテゴリ

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

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by