Need to get dimensions to match and to return a 0 x 0 matrix for empty row.

This code give me a 1 x 0 array for any row that is empty. I need it to return a 0 x 0 empty matrix for any row that is empty. It is presently giving me an error and proclaiming that the dimensions of the object in the "if" statement don't match.
function S = logipack( V)
m = size(V, 1);
S = cell(m, 1);
for r = 1:m
S{r} = find(V(r, :));
if find(V(r,:))==[]
S{r}= [];
end
end
end

댓글 수: 5

What data type is V?
Geoff Hayes
Geoff Hayes 2016년 12월 16일
편집: Geoff Hayes 2016년 12월 16일
DJ - this is the third time that you have posted a question that deals with the same problem of writing the logipack function. Why not continue at https://www.mathworks.com/matlabcentral/answers/317178-what-is-wrong-with-this or https://www.mathworks.com/matlabcentral/answers/317062-told-to-ask-a-new-question-why-won-t-this-work?
Also, consider using the MATLAB debugger to step through the code to understand what is happening. See https://www.mathworks.com/help/matlab/debugging-code.html for details.
DJ V
DJ V 2016년 12월 17일
편집: DJ V 2016년 12월 17일
Because each question represents some advancement, as I was advised earlier by another poster. I am therefore posting each stage of advancement, in the hope of attracting helpful attention. It is a matter of coming to understand how to do specific things, such as saving an empty matrix with dimensions 0 x 0 and not violating how Matlab represents this in terms of syntax.
DJ V
DJ V 2016년 12월 17일
편집: DJ V 2016년 12월 17일
V is a matrix of 1's and 0's. IF there is a 1 in a row the logipack software is supposed to identify the column numbers where it occurs. If there are no 1's, the software is supposed to return an empty vector of size 0 x 0.
@DJ V: You discus the problem "find(V(r,:))==[]" here, although the answer I've given at https://www.mathworks.com/matlabcentral/answers/317178-what-is-wrong-with-this#answer_247497 contains the correct isempty() already. By this way, you do not catch attraction, but discourage readers to care about your questions.

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

 채택된 답변

DJ
MATLAB moans, not because of if condition mismatch, but because the operator '==' doesn't work for cells, try this:
cell1 =
'a' [1] '9' [2x2 double]
>> cell2={'b' 2}
cell2 =
'b' [2]
>> cell1==cell2
Undefined operator '==' for input arguments of type 'cell'.
So,
1.- your 1st line in the for loop doesn't make much sense,
S{r} = find(V(r, :))
so I changed it to
S{r} = V(r, :)
may be it's not what you want but at least now S is not empty.
2.- instead of
if find(V(r,:))=={}
try
if isempty(V(r,:))
or better
if isempty(V(r))
So the following, although not sure whether of much use, at least it doesn't crash
V=randi([-10 10],1,10)
m = size(V, 2);
S = cell(m, 1);
for r = 1:m
S{r} = V(r);
if isempty(V(r))
S{r}= [];
end
end
if you find this answer useful would you please be so kind to mark my answer as Accepted Answer?
To any other reader, please if you find this answer of any help solving your question,
please click on the thumbs-up vote link,
thanks in advance
John BG

댓글 수: 3

Not correct, in fact, your code undoes what I'm trying to do.
S{r} = find(V(r, :))
is central to the solution.
Isempty does work, but it again assigns a 1 x 0 empty vector and not a 0 x 0 empty vector. That is the problem to which I need to find a solution. Nothing else. I'll post the code again as it now stand, with the 1 x 0 empty vector being inserted for appropriate lines:
function out = logipack(V)
m = size(V, 1);
S = cell(m, 1);
for r = 1:m
S{r} = find(V(r, :));
m = size(V, 1);
if isempty(V(r))
S{r}= [];
end
end
out = S;
end
Just in case anyone is curious, this code works and passes the grader:
function out = logipack(V)
m = size(V, 1);
S = cell(m, 1);
for r = 1:m
S{r} = find(V(r, :));
end
for r = 1:m
for q = 1:m
if q==1
count=0;
end
if V(r,q)==0
count= count+1;
end
if count==m
S{r,1}=[];
end
end
end
out = S;
end
happy to see you have managed to answer your own question, crack on

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

추가 답변 (1개)

the cyclist
the cyclist 2016년 12월 16일
You can probably take advantage of the isempty command here.

카테고리

도움말 센터File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

질문:

2016년 12월 16일

댓글:

Jan
2016년 12월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by