How can I query whether a cell is full?
Show older comments
Hi
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
8 Comments
Riccardo Scorretti
on 2 Apr 2022
Your question is too vague; could you provide an example of what do you mean for "cell"?
Berfin Çetinkaya
on 2 Apr 2022
Jan
on 2 Apr 2022
"Cell" means cell arrays in Matlab. You mean a numerical matrix. Now explain, what "full" means: not zero? Not a NaN?
Berfin Çetinkaya
on 2 Apr 2022
the cyclist
on 2 Apr 2022
Maybe you could give a small example of a matrix that is "full", and one that is not.
Berfin Çetinkaya
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
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.)
KALYAN ACHARJYA
on 2 Apr 2022
Edited: KALYAN ACHARJYA
on 2 Apr 2022
If A as an Normal array having size rows=2 & column=2
A=[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'}
Where
A{2,1} is an empty and other having cell ememnts ('whch you may referring as "Full")
Hope it Helps! :)
Answers (1)
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);
else
result = [];
fprintf('A(%d,%d) does not exist: size(A) = [%d %d]',ii,jj,m,n);
end
end
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!