When I switch a double variable to a cell array the dimensions are wrong

I have a 4 by 4 double that I want to switch to a cell array, so i can search for the occurrence of a number.
lets A be the 4 by 4 double. using this line of code.
A_cell = cellstr(num2str(A));
I get a 4 by 1 cell array, by I want 4 by 4 cell array so I search through the numbers and find the occurrence. cheers.

 채택된 답변

Walter Roberson
Walter Roberson 2018년 8월 24일
편집: Walter Roberson 2018년 8월 24일
A_cell = sprintfc('%g', A);
I do not understand how this will help you search for an occurrence of a number, though. If you want to search for a number, == or ismember() or ismembertol() is usually the better choice.

댓글 수: 4

snice the numbers in the 4 by 4 double range from 1-3, i would use strfind to measure the occurrence, then compare the lengths of the arrays. anyways worked like a charm, thanks.
?? Do you mean something like you are trying to find how many leading digits are the same for the double precision values?
It turns out my idea does not work like you predicted, I end up with a 4 by 4 cell array, with 1s and 0s, 1 means the number is there, but it doesn't tell me the number of ones.
ill try to explain my problem in a better way, so i have a 4by4 double, with values from 0-3, 0 just means that the loop has not completed yet, 1-3 corresponds to a cluster, there is condition that will assign each point to the corresponding cluster, but if that condition is the same for two clusters, i want matlab to assign that point to the lowest occurrence cluster so far. this is why i want to count the number of times each number showed up, to pick the lowest occurrence cluster.
cheers
Just compare numeric values in that case; there is no point converting to character for that.
To count the number of times each item showed up, you can use histc() or accumarray()
counts = accumarray( A(:) + 1, 1 );
The first entry will correspond to the number of 0, the second to the number of 1, and so on.

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

추가 답변 (0개)

카테고리

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

태그

질문:

2018년 8월 24일

댓글:

2018년 8월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by