I have a cell named "r2" which has 2 rows and 1 column:
r2 {1,1} = 1046 x 1
r2 {2,1} = 1045 x 1
When analyzing the number of repetitions in a cell how in the following example:
r2 = {[1, 4, 3, 1], [2, 2, 3]};
I should have results:
r3 = {[1 2, 2 0, 3; 1 4 1], [1 0, 2; 2 3 1]};
In applying the code:
if true
% code
r2 = {[1, 4, 3, 1]; [2, 2, 3]};
r3 = cellfun (@ (x) [(1: máx (x)) ', accumArray (x (:), 1) ], r2, 'un', 0);
end
Matlab gives me the error:
*"Error using accumarray
First input SUBS must contain positive integer subscripts.
Error in @(x)[(1:max(x))',accumarray(x(:),1)]"*
How I can fix it?
Many thanks

 채택된 답변

Walter Roberson
Walter Roberson 2013년 12월 15일

0 개 추천

invalid_entries = cellfun( @(x) x(x <= 0 | mod(x,1) ~= 0), r2, 'Uniform', 0);
Use that to find out which values are not positive integers.

댓글 수: 4

FRANCISCO
FRANCISCO 2013년 12월 15일
The value that is not positive is "0". How I can solve?. I've thought about replacing the "0" with "100" that I can solve
Walter Roberson
Walter Roberson 2013년 12월 15일
What behavior would you like to see for the 0? Would you like the 0 to be counted or omitted ?
FRANCISCO
FRANCISCO 2013년 12월 15일
I consider the "0", then replaced it with "100" and then I had it in mind to place it in the cell, returning to change the "100" to "0". Many thanks
r3 = cellfun (@ (x) [(0: máx (x)) ', accumArray (x (:) + 1, 1) ], r2, 'un', 0);
Notice the +1 added to the x(:) so that 0 becomes index 1, 1 becomes index 2, and so on.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품

태그

질문:

2013년 12월 15일

댓글:

2013년 12월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by