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 件のコメント

The big YeeT
The big YeeT 2018 年 8 月 24 日
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.
Walter Roberson
Walter Roberson 2018 年 8 月 24 日
?? Do you mean something like you are trying to find how many leading digits are the same for the double precision values?
The big YeeT
The big YeeT 2018 年 8 月 24 日
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 で Characters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by