original question is here: http://www.mathworks.com/matlabcentral/answers/316721-i-don-t-understand-this-question-does-someone-else
Told to ask a new question = Why won't this work?
2 views (last 30 days)
Show older comments
My code follows, it doesn't work, but it does store the column indices of 1s.
function SQUARE = logipack( V)
% Find the size of the input matrix
[m,n] =size(V);
% For each row in vector..
counter = 0;
for count = 1:m
for incount=1:m
% Get the cell that corresponds to the column positions that we
% want to look at
counter=0;
if V(count,incount)>0
counter = counter+1
SQUARE(count,counter)=incount;
end
end
end
end
The question is:
%
2 Comments
Answers (1)
Geoff Hayes
on 15 Dec 2016
DJ - you will have to clarify what you mean by it doesn't work. What is the input that you are using and what is the output (that is incorrect)? For example, if I do
>> logipack(eye(4))
the answer is
ans =
1
2
3
4
which is almost what I expect. (Almost because the output isn't a cell array.) However, if I pass in a matrix of all zeros like
>> z = logipack(zeros(4,4))
I observe the following error
Output argument "SQUARE" (and maybe others) not assigned during call to...
Which makes sense since your code only considers what will happen if there is a one in your row. Since the output parameter is SQUARE then you should initialize it so that an answer is always returned. Also, remember that the output is to be a cell array where each element in the cell array is either a numeric array of increasing indices or an empty array (if no ones exist). Try initializing SQUARE as
function SQUARE = logipack( V)
% Find the size of the input matrix
[m,n] =size(V);
SQUARE = cell(m,1);
The next thing to consider is that for each row of V you must build an array of indices and then assign that array to the corresponding row of SQUARE. So do that first:
for r=1:m
for c=1:n % note here that I am using n and not m like in your code
% create an empty array
if V(r,c) > 0
% concatenate the correct index to your array
end
% update the appropriate cell array element with the array of indices
end
end
You just need to fill in the blanks! :)
3 Comments
Geoff Hayes
on 16 Dec 2016
A cell array allows you to hold objects of different types within it's cells. So rather than having an array of all doubles or all chars, you can have a cell array of a mixture of different data types. In your case, you want each cell to hold a numeric array.
The
SQUARE = cell(m,1);
initializes SQUARE to be an array with m rows and one column which is exactly what you need for your output since you must iterate over each row of V and determine those indices which correspond to non-zero elements.
I realize now that my comments (in the above code were incorrectly placed)
for r=1:m
% create an empty array
for c=1:n % note here that I am using n and not m like in your code
if V(r,c) > 0
% concatenate the correct index to your array
end
end
% update the appropriate cell array element with the array of indices
end
You want to create an empty array before you iterate over each column in the row. Once you find an non-zero element at V(r,c), you will take its index (which is c) and add it to this array. Once you have iterated over every column (for row r) you will take that array and insert it into your cell array (at row r).
Remember, each cell of SQUARE has either a non-empty array or an array with at least one element that corresponds to the indices of non-zero elements in V.
See Also
Categories
Find more on Logical 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!