What is wrong with this?

1 回表示 (過去 30 日間)
DJ V
DJ V 2016 年 12 月 16 日
コメント済み: Jan 2016 年 12 月 16 日
I need to be able to determine what my code is producing.
It yields:
How do I look into the cells that are listed to get it to print out the results?
The code that produces this is:
function SQUARE = logipack( V)
% Find the size of the input matrix
[m,n] =size(V);
SQUARE = cell(m,1);
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
counter=0;
if V(r,c) > 0
counter = c;
end
if counter >=1
SQUARE{r,counter}=cell(counter);
end
end
end
end
This problem is described as:
%
  2 件のコメント
DJ V
DJ V 2016 年 12 月 16 日
Note: This code doesn't seem to work. Can anyone advise me?
Geoff Hayes
Geoff Hayes 2016 年 12 月 16 日
DJ - rather than posting duplicate questions, why not further the conversation at http://www.mathworks.com/matlabcentral/answers/317062-told-to-ask-a-new-question-why-won-t-this-work?

サインインしてコメントする。

採用された回答

Jan
Jan 2016 年 12 月 16 日
編集済み: Jan 2016 年 12 月 16 日
A cell vector is wanted. SQUARE{r,counter} creates a cell matrix.
function S = logipack( V)
m = size(V, 1);
S = cell(m, 1);
for r = 1:m
data = find(V(r, :)); % [EDITED]
if ~isempty(data)
S{r} = data;
end
end
end
  2 件のコメント
DJ V
DJ V 2016 年 12 月 16 日
編集済み: DJ V 2016 年 12 月 16 日
But the empty matrix where applicable must be a 0x0 matrix. This code returns a 1x 0 matrix. I attempt to fix, but it gives me an error and proclaims that the matrix dimensions don't agree. The code returns a 1x0 matrix for an empty row.
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
Jan
Jan 2016 年 12 月 16 日
See [EDITED]

サインインしてコメントする。

その他の回答 (1 件)

DJ V
DJ V 2016 年 12 月 16 日
I modified code to:
function SQUARE = logipack( V)
% Find the size of the input matrix
[m,n] =size(V);
SQUARE = cell(m,1);
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
counter=0;
if V(r,c) > 0
counter = c;
end
if counter >=1
SQUARE{r,counter}=counter;
end
end
end
end
This produces:
This appears to be correct.
But the grader says my result is wrong for [1 0; 0 1].
Why?
  2 件のコメント
DJ V
DJ V 2016 年 12 月 16 日
Sorry, it says its wrong for [0 1; 1 0].
Steven Lord
Steven Lord 2016 年 12 月 16 日
The assignment says the output should be a logical array. The output from your function is a cell array. A cell array could contain a logical array, but it is not itself a logical array.
C = {true false true};
isa(C, 'logical') % it is not a logical array
isa(C{1}, 'logical') % it contains a logical array

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrix Operations and Transformations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by