I don't understand this question, does someone else?
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
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.)
0 个评论
更多回答(3 个)
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
0 个评论
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).
0 个评论
DJ V
2016-12-14
1 个评论
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 Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!