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.

 Accepted Answer

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 Comments

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.

Sign in to comment.

More Answers (0)

Categories

Tags

Community Treasure Hunt

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

Start Hunting!