Cell arrays and Indexing with Cells HELP?

21 views (last 30 days)
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...
function answer = hw5_problem4(Q)
X = length(Q);
Z = zeros(X, X);
row = Q(1:end);
col = Q{1:end};
Z(row, col) = true;
answer = Z;
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?
PLEASE HELP :)
  1 Comment
James Metz
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:
function answer = hw5_problem4(Q)
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
answer = Z;
end
Please help!!!

Sign in to comment.

Accepted Answer

James Tursa
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.
  3 Comments
James Tursa
James Tursa on 4 Apr 2020
Too complicated and too many loops. You only need one loop as I have shown, and you only need one line within that loop.
for ii=1:X
% One line of code here to set the appropriate values in this row of Z to true based on Q{ii}
end
Hints about that one line of code:
Z(1,[2 4 7]) are the elements Z(1,2) and Z(1,4) and Z(1,7)
Z(5,[3 5]) are the elements Z(5,3) and Z(5,5)
I.e., you can use a vector of indexes for the columns. Knowing this, ask yourself what Q{1} and Q{5} contain. They are vectors of column indexes, right? And Q{ii} is a vector of column indexes for row ii, right? See if you can take this information to write that one line of code.
Also this
Z = zeros(X, X);
needs to be this
Z = false(X, X);
since you want a logical output.
James Metz
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:
function answer = hw5_problem4(Q)
X = length(Q);
Z = false(X, X);
for ii = 1:X
Z(ii,Q{ii}) = true;
end
answer = Z;
end
Thanks to @James Tursa for the help.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!