I don't understand this question, does someone else?

조회 수: 2 (최근 30일)
DJ V
DJ V 2016년 12월 13일
댓글: Matthew Murphy 2016년 12월 14일
The question is:
Can someone please explain it via an illustrative example?
Hands off if you happen across this question and decide its not up to your personal standards. I need some help, and the class is on-line and closed.
  댓글 수: 2
DJ V
DJ V 2016년 12월 13일
That is to say the class has ended.
Matthew Murphy
Matthew Murphy 2016년 12월 14일
Please accept the answer to this question that has helped you the most.

댓글을 달려면 로그인하십시오.

채택된 답변

Geoff Hayes
Geoff Hayes 2016년 12월 13일
DJ - suppose the input cell array (vector) contains 3 elements (so n=3). Each element is an array that contains the indices (in increasing order) for those elements in the logical nxn matrix that are true (or one, since the logical nxn array has elements that are zero or one).
cArray = {[1] ; [1 2] ; [2 3]};
If we were to call
>> logiunpack(cArray)
then the output logical array would be
ans =
[1 0 0;
1 1 0;
0 1 1]
The first element of cArray is [1] which tells us that the only true element in the first row of the logical array is one (the rest are zeros). The second element of the cell array has two elements, 1 and 2 which tells us that the first and second elements of the second row of the logical array are true. Finally, the third element of the cell array is a two element array with the values of 2 and 3 which means that the second and third elements of the third row of the logical array are true (one).
(At least, that is my interpretation of the problem.)

추가 답변 (3개)

Guillaume
Guillaume 2016년 12월 13일
It's very clear to me. Example input:
n = 5;
C = {[1 3 4]; [2 5]; []; 4; 1:5};
Desired output:
tf = logical([1 0 1 1 0 .... true value at index 1, 3, and 4
0 1 0 0 1 .... true value at index 2 and 5
0 0 0 0 0 .... false everywhere
0 0 0 1 0 .... true value at index 4
1 1 1 1 1]) % true value at all indices

Matthew Murphy
Matthew Murphy 2016년 12월 13일
So I'm going to work through this stream-of-consciousness, so no guarantees. Let's say the square matrix is a 5x5. Then, the cell vector is a 5x1 array with each cell as a 1x5 double array.
Now, let's say that original 5x5 had "true" values in the end elements of each row; indices 1 and 5 of each row are true. Then, our cell array would have cells that look like this:
(The last element may be a 2 or a 5 - slightly unsure of the wording in the question, so I'll address both cases.)
row1 = [1 false false false 5];
row2 = [1 false false false 2];
Since you are making a square matrix, you can initialize an all-false matrix based on the size of the input cell vector. Given that vector, we would want to expand each cell into the double array and go by cases then.
Case 1 (row1): In this case, a non-false value corresponds to a true value in the column given by the value in that place.
Case 2 (row2): In this case, a non-false value corresponds to a true value in the column given by the index of the value in that place.
So all in all, instantiate new matrix, unpack cell vector, loop through created arrays, place true values in the positions indicated by the cases.
You may have to modify slightly based on the dimension of the input cell vector (column or row vector).

DJ V
DJ V 2016년 12월 14일
Thanks.
I have to pack the matrix in an array.
Here is my code.
It isn't working. Any idea what is wrong?
function SQUARE = logipack( V)
% V is a cell array of vectors. Think of a cell array as an array where
% each element can contain ANYTHING. The first element could be a vector,
% the second element could be a matrix and the third element could be a
% string. The point is that each element in the cell array can be something
% different, not limited to just single values.
%
% You access cell contents using the curly braces, {}
% If V is a cell array, doing V{2} would give you the second cell array element
% Doing V{4} would give the fourth and so on. Writing to a cell is similar.
% If you did V{1} = a;, it will write the contents of variable a to the first
% position of the cell array V. You can access groups of
% cells by using the round braces, so doing V(1:4) accesses the cells from
% positions 1 to 4 and gives you a new cell array that contains this subset
% Find the total number of cells
[m,n] =size(V);
% Pre-allocate a matrix that is all false and we will only modify
% the positions that V dicates for us
% For each cell we have...
counter = 0;
for count = 1:m
for incount=1:m
counter = counter+1;
% Get the cell that corresponds to the column positions that we
% want to modify
if count~= incount
if V(count,incount) ==true && V(incount,count)==true
S{incount}=[1,1];
end
if V(count,incount)==false && V(incount, count)==true
S{incount}=[0,1];
end
if V(count,incount)==true && V(incount,count)==false
S{incount}=[1,0];
end
if V(count,incount)==false && V(incount,count)==false
S{incount}=[0 0];
end
end
if count== incount
if V(count,incount)==true && V(m-(incount-1),m-(incount-1))==true
S{incount}=[1,1];
end
if V(count,incount)==false && V(m-(incount-1),m-(incount-1))==true
S{incount}=[0,1];
end
if V(count,incount)==true && V(m-(incount-1),m-(incount-1))==false
S{incount}=[1,0];
end
if V(count,incount)==false && V(m-(incount-1),m-(incount-1))==false
S{incount}=[0 0];
end
end
if counter <=ceil(m^2/2);
SQUARE{counter} = S(incount);
end
end
end
end
ignore the comments, they're from the unpack program.
The pack program description is:
%
  댓글 수: 1
Guillaume
Guillaume 2016년 12월 14일
Please, don't ask questions in an answer. Make a new question
You can make your program a lot more readable (in my opinion) using the fact that
if something == true
%...
end
is the same
if something
%...
end
and
if something == false
%...
end
is the same as
if ~something
%...
end
I have no ideas why you're trying to store 0s and 1s when you're supposed to store the column indices of the true values.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by