Clear Filters
Clear Filters

How can I query whether a cell is full?

8 views (last 30 days)
Berfin Çetinkaya
Berfin Çetinkaya on 2 Apr 2022
Answered: Voss on 2 Apr 2022
How can I query whether a cell is full? For example, I want to do something like this using the if structure: if this cell is full, do the action, if not, don't.
thank you for help
Steven Lord
Steven Lord on 2 Apr 2022
It is not possible for an array in MATLAB to have a "hole" (unless the array is a Java array.) So this would be invalid syntax:
A = [1 2; 3] % The second row must have two elements
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
By "full" do you mean that the lower-right corner element of your matrix is non-zero? So [0 0; 0 1] would be "full" but [1 2; 3 0] would not?
Or do you mean that each row and each column has at least one non-zero element, so that the matrix must be at least as large as it is to contain the data? An example of a matrix that is "non-full" by that latter definition is [1 0; 0 0]. The last row and column aren't actually necessary if all you want to do is to store that one non-zero element 1, you can do it with [1].
Part of the confusion, I think, is that "full" in MATLAB usually means "not sparse", i.e. issparse returns false.
Perhaps if you explain how you are planning to use the information about whether a matrix is "full" or not in your program, or what the purpose of your program is, we might be better able to understand how to make that determination (or a way to solve the problem without needing to make that determination.)
Edited: KALYAN ACHARJYA on 2 Apr 2022
If A as an Normal array having size rows=2 & column=2
A=[3 4;8 5]
A = 2×2
3 4 8 5
Here A(3,2) having no sense, as index exceeds matrix dimension. As in matrix there is no empty elements, either it should be numbers or NaN, but it must be.
Hence calling any matrix elements which is out of Matrix dimension have no sense.
On the other hand in cell array which is possible
Lets suppose A is cell array having sizes 2x2
A={2,3;[] 'Kalyan'}
A = 2×2 cell array
{[ 2]} {[ 3]} {0×0 double} {'Kalyan'}
A{2,1} is an empty and other having cell ememnts ('whch you may referring as "Full")
Hope it Helps! :)

Sign in to comment.

Answers (1)

Voss on 2 Apr 2022
Based on the comments it sounds like you need to use size to check whether the given row and column indices are valid for the given matrix.
A = [3 4; 8 5];
[m,n] = size(A);
rows_to_test = [2 3];
cols_to_test = [2 2];
for kk = 1:numel(rows_to_test)
ii = rows_to_test(kk);
jj = cols_to_test(kk);
% check that indices ii and jj are both valid:
if ii >= 1 && ii <= m && jj >= 1 && jj <= n
result = A(ii,jj);
fprintf('A(%d,%d) = %f',ii,jj,result);
result = [];
fprintf('A(%d,%d) does not exist: size(A) = [%d %d]',ii,jj,m,n);
A(2,2) = 5.000000
A(3,2) does not exist: size(A) = [2 2]


Community Treasure Hunt

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

Start Hunting!