For the second part of the question, I keep on getting the following error. I am assuming this is because I need to somehow create a cell inside the cell corresponding to Out? What would be the simplest way of doing this?

1 visualizzazione (ultimi 30 giorni)
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 representing the column indexes of the logical true values in the given row of the matrix. All other elements in the given row of the logical matrix are false. Write a function called logiunpack 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 cell vector at index 10. That element is the vector [20 75].
Solve the inverse of the previous problem. That is, write a function called logipack that takes a square logical matrix as its only input argument and returns its cell vector representation as specified in the previous assignment. Note that empty array elements of the cell vector corresponding to rows with all false values must have a size of 0x0.
function Out=logipack(M)
[row,col]=size(M);
Out=cell(1,length(M));
for i=1:row
for j=1:col
if (sum((M(i,:)==0)))==0
return
elseif M(i,j)==1
Out{i}=j; %how to create cell inside cell?
end
end
end
end
Problem 6 (logipack):
Feedback: Your function performed correctly for argument(s) [0 1;1 0]
Feedback: Your function made an error for argument(s) [1 0 0;1 1 1;0 0 0]
Your solution is _not_ correct.
  3 Commenti
champions2015
champions2015 il 6 Set 2017
Modificato: James Tursa il 6 Set 2017
I'm trying to create a separate sub vector and then simply insert it in out(I), but can't seem to figure out how to do this small part. This is what I've got now:
function Out=logipack(M)
[row,col]=size(M);
Out=cell(1,length(M));
for i=1:row
for j=1:col
vector=[];
if (sum((M(i,:)==0)))==0
return
elseif M(i,j)==1
vector(i)=j;
Out{i}=vector(i);
end
end
end
end
OCDER
OCDER il 6 Set 2017
I think the function you want to use somewhere is "find". It returns the indices of where the ones are.
find([1 0 1 1 0]) %Returns [1 3 4]

Accedi per commentare.

Risposta accettata

James Tursa
James Tursa il 6 Set 2017
Modificato: James Tursa il 6 Set 2017
You've still got that j-loop too complicated. Use my outline above. Using your vector=[] approach, it would be:
vector = [];
for j=1:col
if M(i,j)==1
vector = [vector j]; % add the value of j to the vector
end
end
Out{i} = vector; % after the j-loop, put the vector into Out
Also, you need your Out variable to have the same number of elements as the input matrix has rows. So this line:
Out=cell(1,length(M));
should be this instead:
Out=cell(row,1);
Now, there are more efficient ways to do this (using the "find" function as suggested by Donald Lee for instance). But you should learn how to make this for-loop method work first.
  2 Commenti
champions2015
champions2015 il 6 Set 2017
Modificato: champions2015 il 7 Set 2017
Ok thanks! It now makes sense how to add onto the vector. However, it seems that I don't get the actual code, and instead get this:
>> logipack([1 0 0;1 1 1;0 0 0])
ans =
3×1 cell array
[ 1]
[1×4 double]
[1×4 double]
when i should be getting a 1x3 cell? I get this same output when i try using 'Out=cell(1,col)' at the start to.
Code:
function Out=logipack(M)
[row col]=size(M);
vector=[];
Out=cell(row,1);
for i=1:row
for j=1:col
if M(i,j)==1
vector=[vector j]; %add the value of j to the vector, could also use find
end
end
Out{i}=vector; %after j loop, put vector into i
end
end
OCDER
OCDER il 7 Set 2017
I can't get that error, but I do think the "vector = []" placement is off. It should be inside the first loop
function Out = logipack2(M)
[row, col] = size(M);
Out = cell(row,1);
for i = 1:row
vector = [];
for j = 1:col
if M(i, j) == 1
vector = [vector j]; %add the value of j to the vector, could also use find
end
end
Out{i} = vector; %after j loop, put vector into i
end
otherwise you'd accumulate indices of all rows.
Also, consider avoiding using "i" as a counter variable, because it is used as the imaginary i in matlab.

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by