Hi,
I would like to make a problem statement in Matalb, here are 3 columns, the 1st column is the user ID, the 2nd is (A/B) and 3rd is the combinations of $ and NIC.
(1) I wanted to count (a) how many many times $ has been pressed in total (b) count multiple $ as once pressed only by each user and total number of it.
(2) And in column 2, i wanted to check how many times the NIC has been pressed before $ pressed (so, 1st pressed: NIC, 2nd pressed: $) and count the total number of users.
I haven't used the Matalb for so long, so i would like to know how to do it...
Tim

 採用された回答

Dyuman Joshi
Dyuman Joshi 2022 年 5 月 19 日

1 投票

Final answer, I have tested this code on my laptop
cd=0; %counting $
userd=[];
for i=1:size(T,1)
if isequal(char(T.Column2(i)),'$')
cd=cd+1; %number of '$' has been pressed.
userd=[userd char(T.Column1(i))];
end
end
uniqued=numel(unique(userd))
cnic=0; %counting NIC before $
usernic=[];
for j=1:size(T,1)-1
if isequal(char(T.Column2(j)),'NIC')&isequal(char(T.Column2(j+1)),'$')
cnic=cnic+1;
usernic=[usernic char(T.Column1(j))];
end
end
uniquenic=numel(unique(usernic))

その他の回答 (3 件)

Dyuman Joshi
Dyuman Joshi 2022 年 5 月 12 日
編集済み: Dyuman Joshi 2022 年 5 月 13 日

0 投票

I'm assuming that the data in Column 1 and Column 2 is char/strings.
%T is your table
cd=0; %counting $
userd=[];
for i=1:size(T,1)
if isequal(T.Column2(i),'$')
countd=countd+1; %number of '$' has been pressed.
userd=[userd T.Column1(i)];
end
end
uniqued=numel(unique(userd));
cnic=0; %counting NIC before $
usernic=[];
for j=1:size(T,1)-1
if isequal(T.Column2(i),'NIC')&isequal(T.Column2(i+1),'$')
cnicbefd=cnicbefd+1;
usernic=[usernic T.Column1(i)];
end
end
uniquenic=numel(unique(usernic));

12 件のコメント

ka chun yick
ka chun yick 2022 年 5 月 12 日
I mean if the user has pressed it mutiple times, and i would like to count as one time only
Dyuman Joshi
Dyuman Joshi 2022 年 5 月 12 日
I have edited my answer
ka chun yick
ka chun yick 2022 年 5 月 12 日
This maybe a stupid question. I have imported all the columns here, but it said Unrecognized function or variable "T" and Error on Untitled (line 4)
ka chun yick
ka chun yick 2022 年 5 月 12 日
On top of that, What do (T,1),(i,3) &(i,2) mean in the table?
ka chun yick
ka chun yick 2022 年 5 月 13 日
@Dyuman Joshi, apologise about that. I have deleted it. This is my second attampt. I have defined the T and here is the another error.
Dyuman Joshi
Dyuman Joshi 2022 年 5 月 13 日
Can you show what the values in T are?
ka chun yick
ka chun yick 2022 年 5 月 13 日
If you look at the right-hand side i exported it from the excel. The table is exactly the same as the original post
Dyuman Joshi
Dyuman Joshi 2022 年 5 月 13 日
I saw that. But I don't know what is the data type. Can you post the data here so I can try it myself directly?
ka chun yick
ka chun yick 2022 年 5 月 13 日
The excel is named as T
Dyuman Joshi
Dyuman Joshi 2022 年 5 月 13 日
編集済み: Dyuman Joshi 2022 年 5 月 13 日
Okay, when you import T, what is the data type of Column 1 and 2? Char/string or categorical?
Asking because I am getting 2 different answers on based on method of importing.
ka chun yick
ka chun yick 2022 年 5 月 13 日
I think it is the simple char here from the excel
Dyuman Joshi
Dyuman Joshi 2022 年 5 月 13 日
I have modified my code accordinly, please check.

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

ka chun yick
ka chun yick 2022 年 5 月 13 日

0 投票

Hi, i got the following error, it said 'countd' is unrecognized......
Unrecognized function or variable 'countd'.
Error in Untitled (line 6)
countd=countd+1; %number of '$' has been pressed.

4 件のコメント

Dyuman Joshi
Dyuman Joshi 2022 年 5 月 13 日
sorry, change cd=0 to countd=0.
ka chun yick
ka chun yick 2022 年 5 月 14 日
@Dyuman Joshi okay, it seems got far more problems once changing it....
Index exceeds the number of array elements (10).
Error in categorical/subsref (line 21)
b.codes = a.codes(s.subs{:});
Error in tabular/dotParenReference (line 108)
b = b(rowIndices);
Error in Untitled (line 14)
if isequal(T.Column2(i),'NIC')&isequal(T.Column2(i+1),'$')
ka chun yick
ka chun yick 2022 年 5 月 18 日
@Dyuman Joshi Any idea?
Dyuman Joshi
Dyuman Joshi 2022 年 5 月 18 日
This is just a data type mismatch in the if condition statement. Check what the data type in table T, then you can edit accorindingly as well.

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

ka chun yick
ka chun yick 2022 年 5 月 19 日

0 投票

@Dyuman Joshi. I am sure when i imported it from excel, it is a table as a whole. i have been reading some documents on how to converting type: https://uk.mathworks.com/help/matlab/data-type-conversion.html .But i still getting really confused on how to check the data and how to edit it from here, also the if condition statement..... Appologise for all the confusions......

カテゴリ

ヘルプ センター および File ExchangeTables についてさらに検索

質問済み:

2022 年 5 月 12 日

回答済み:

2022 年 5 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by