# Cell arrays and Indexing with Cells HELP?

5 views (last 30 days)
James Metz on 3 Apr 2020
Commented: James Metz on 4 Apr 2020
An assignment I have says...
"An n-by-n square logical matrix can be represented by a cell vector of n elements where the kth element corresponds to the kth row of the matrix. Each element of the cell vector is a row vector of positive integers in increasing order reperesenting the column indexes of the logical true values in the given row of the matrix. All other elements in the given row of the ligical matrix are false. Write a function that takes such a cell vector as its only input and returns the corresponding square logical matrix. For example, such a cell vector representation of a 100-by-100 logical matrix with the only true elements at indexes (10,20) and (10,75) would have only one non-empty element of the 100-element cell vector at index 10. that element is the vector [20 75]."
I understand the input is the "indices" (non-traditional since they just give the location within each row, instead of within the entire array) at which the function should output logical trues; however, I am having a difficult time figuring out how to actually index into the zeros array my function creates using these indices. Here's an example of what the function is supposed to do...
for n = 3 (also length(A))
function output = input(A)
A = {[1 3], [], [2]}
output =
1 0 1
0 0 0
0 1 0
***notice that the size of the matrix is a 3x3 because that was the length of input A.
Here's my attempt, although I'm rather new to MATLAB so there are likely many mistakes...
X = length(Q);
Z = zeros(X, X);
row = Q(1:end);
col = Q{1:end};
Z(row, col) = true;
return
end
***notice I created a zeros matrix and then attempted to "update" the matrix by indexing into the matrix and changing all the falses in those locations to trues; however, the error message I keep recieving is...
Unable to use a value of type 'cell' as an index.
Error in hw5_problem4 (line 6)
Z(row, col) = true;
Since Q is a cell vector I though Q(1:end) would work but I guess it doesn't?
James Metz on 4 Apr 2020
Ok, so apparently this function can be done using a nested for loop. I wasn't sure how indexing would work with a for loop but I tried it out and this is what I came up with. It still doesn't work, but it's better than what I had before:
X = length(Q);
Z = zeros(X, X);
for ii = 1:X
for jj = 1:X
if ii == Q(ii) && jj == Q{ii}
Z(ii, jj) = true;
end
end
end
end

James Tursa on 4 Apr 2020
Edited: James Tursa on 4 Apr 2020
This
row = Q(1:end);
col = Q{1:end};
Z(row, col) = true;
is actually a good attempt and shows you understand the problem ... it just doesn't fit with MATLAB required syntax.
What if you took a step back and instead worked on only one row at a time? E.g., write a loop over the rows:
for row=1:X
% Q{row} is a vector of indexes that are true in this row
% Write code here to set the appropriate values in this row of Z to true
end
Remember that you can use vectors of indexes directly as subscripts. Give this an attempt and come back if you have more problems.
Alternatively, you could use linear indexing, either manually or using the sub2ind( ) function.
James Metz on 4 Apr 2020
Ah, it finally worked! You deserve the best, sir. I can't thank you enough for your help. Sometimes I get too ahead of myself and make it harder than it needs to be.
For anyone looking for the correct code for this question:
X = length(Q);
Z = false(X, X);
for ii = 1:X
Z(ii,Q{ii}) = true;
end
end
Thanks to @James Tursa for the help.