To calculate value in a uitable

1 view (last 30 days)
In a uitable table, I have produce a matrix as below:
>> a= [1 1 1 2 2 3 3 3 4 ; 3 3 1 4 4 4 3 1 1; 3 3 2 4 1 1 1 3 2]
a =
1 1 1 2 2 3 3 3 4
3 3 1 4 4 4 3 1 1
3 3 2 4 1 1 1 3 2
Therefore,in each row, I need to calculate the marks occur based on conditions below:
1. if no 3 occur in three cells continuously therefore the mark charged is 30.
2. if no 3 occur in two cells continuously and then followed by no 1 in the next cell, the mark charged is -20.
3. if no 3 occur in a cell and then followed by no 1 in the next cell, the mark charged is -10.
4. if no 3 occur in two cells continuously and then followed by no 2 in the next cell, the mark charged is -20.
5. if no 3 occur in a cell and then followed by no 2 in the next cell, the mark charged is 0.
Then, in each row, the total marks will calculate. Finally the result will be save in a matrix like this:
marks =
-30
-30
-20
This marks also will total to become an X value as below:
X = -80
How can I code this problem in matlab? Please help me and thank you.

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 21 Dec 2011
try this code, I cant check now
s = size(a);
b = zeros(s);
b(abs(a - 3) < 1e3*eps) = -10;
k = [true(s(1),1) diff(a,1,2)~=0].*a;
p = arrayfun(@(x)10*nnz(strfind(k(x,:),[3 2])),(1:s(1))');
marks = sum(b,2) + p;
  3 Comments
Andrei Bobrov
Andrei Bobrov on 21 Dec 2011
Hi Yue!
X = sum(marks);

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!