Counting number of 'negative' cells for each ID - matlab

I want to count how many 'negative' cells there are for each unique ID. What am I doing wrong here? I then want to display how many ID's have one or more negative counts and how many ID's have three or more negative counts.
my code:
clc
clear
close all
data = {
'vajbfch07wu11y90' 'negative'
'ekvpmhqybu920hnr' 'negative'
'eddbdv5bsqggeydq' 'negative'
'eddbdv5bsqggeydq' 'negative'
'eddbdv5bsqggeydq' 'negative'
'eddbdv5bsqggeydq' 'negative'
'eddbdv5bsqggeydq' 'negative'
'eddbdv5bsqggeydq' 'negative'
'6vmhbj7041fe7xck' 'negative'
'6vmhbj7041fe7xck' 'negative'
'6vmhbj7041fe7xck' 'negative'
'6vmhbj7041fe7xck' 'negative'
'j5d6dgzbeynyyg02' 'negative'
'j5d6dgzbeynyyg02' 'negative'
'j5d6dgzbeynyyg02' 'negative'
'v21tstcp164uya6g' 'negative'
'v21tstcp164uya6g' 'negative'
'brjmfpunj00rn92c' 'negative'
'brjmfpunj00rn92c' 'negative'
'mb8nggnegcwq9nqc' 'negative'
'vk4ga34492m01hwv' 'negative'
'vk4ga34492m01hwv' 'negative'
'vk4ga34492m01hwv' 'negative'
'vk4ga34492m01hwv' 'negative'
'd9hk2zeexhxp2h0v' 'negative'
'f93xk5sq60ehp34j' 'negative'
'f93xk5sq60ehp34j' 'negative'
'ypzzn212hqvwjtc9' 'negative'
'0q2mmnq0wb97z7bm' 'negative'
'jh8k1dd9g2p2d218' 'negative'
'2e5tr0scw89z68kg' 'negative'
'2e5tr0scw89z68kg' 'negative'
'5zb72reqnsxnzuca' 'negative'
'5zb72reqnsxnzuca' 'negative'
'5zb72reqnsxnzuca' 'negative' };
[sigma, ~, uidx] = unique(data(:,1));
counts = accumarray([uidx, 2-data(:,2)], 1);
sigma = [sigma, counts];
sigma
error message:
Operator '-' is not supported for operands of type 'cell'.
Error in count_ex (line 45)
counts = accumarray([uidx, 2-data(:,2)], 1);

You could use the new dictionary type in R2022b to count occurences and at the same time get unique items:
d = dictionary(data(:, 1), 0);
for entry = transpose(data(:, 1));
d(entry) = d(entry) + 1;
end
If that is not available:
It seems all entries are labeled 'negative', so you just need to count occurences in column one, see https://de.mathworks.com/matlabcentral/answers/115838-count-occurrences-of-string-in-a-single-cell-array-how-many-times-a-string-appear
c = categorical(data(:, 1));
[categories(c), num2cell(countcats(c))]

Related

index 398120 is out of bounds for axis 0 with size 398120

I am trying to filter a numpy array with the code below. but I get this error: index 398120 is out of bounds for axis 0 with size 398120, how can I solve it?
for n in range(m):
if n == 0:
t1dc[n]=t1[n]
t2dc[n]=t2[n]
t3dc[n]=t3[n]
else:
t1dc[n]=0.0158*t1[n]+0.9843*t1dc[n-1]
t2dc[n]=0.0158*t2[n]+0.9843*t2dc[n-1]
t3dc[n]=0.0158*t3[n]+0.9843*t3dc[n-1]

Filtering out rows of data based on criteria from different columns in MATLAB

I have data from an experiment in which each row is a different trial. The table has several columns with information about the trial, and then 1000 columns of data.
I need to filter out certain trials (rows) based on various criteria in order to calculate means from valid trials only. For example, I have a column with accuracy data (0 = error, 1 = correct), and a column with response time data (need to filter out responses < 200). I assume I need to loop through each row to create a new true/false logical column based on my criteria (e.g., valid trials are accuracy == 1 & RT > 200).
Then, I need to create means of only valid trials grouped by conditions which are defined by strings in another column (e.g., create 3 means based on a valence column with "negative", "neutral" and "positive"). I know this is probably relatively simple to do, I am new to MATLAB. All suggestions appreciated.
Valence Acc RTs NumNan Data1
'Negative' 1 540 0 278.5148611
'Negative' 1 597 0 89.18152778
'Negative' 1 381 0 173.5148611
'Negative' 1 471 0 19.51486111
'Negative' 1 535 0 2.514861111
You can operate with boolean logic.
Suppose your data is stored in matrix A.
cond1 = A(:,2) == 1; % only select 'accurate' data
cond2 = A(:,3) < 200; % response time <200
cond3 = A(:,1) ~= 'Negative'; % non-negative trials
idx = cond1 & cond2 & cond3; % use 'AND' logic to find indices of satisfying records
B = A(idx,:); % pick out selected records and save in new matrix

Sort 3D matrix depending on specific column

I have 3D matrix (10*10*4) and I am trying to short each slice depending on the 2 column after that and depending on column 2 I want to delete all rows that have a value on column 2 less than 1 and greater than 17.
I used this code to sort but it is not working
clc;
clear;
A = rand(10,10,4)
column = 2;
[values,indices] = sort(A(:,column,:))
B = A(indices,:,:)
Thanks
Here is the code for sorting.
clc;
clear all;
A = rand(10,10,4);
column = 2;
z=A(:,column,:);
[values,indices] = sort(z,1) ;
B=zeros(size(A,1),size(A,2),size(A,3));
for i=1:size(A,3)
B(:,:,i) =A(indices(:,:,i),:,i);
end
In here all values in A and B are between 0 and 1. So no point in checking less than 1 and greater than 17. Another thing is that when you delete a row, what are you going to do with the third dimension? If you do the deletion for each third dimension separately B(:,:,1), B(:,:,2), B(:,:,3), B(:,:,4). Each dimension B(:,:,1), B(:,:,2), B(:,:,3), B(:,:,4) will have different number of rows. B cant have different number of rows like that.
Updated: code with sorting and delete rows when all values in 2nd column of that row is less than 0.05 or all values in 2nd column of that row is greater than 0.15
clc;
clear all;
A = rand(10,10,4);
%% sorting
column = 2; % sort base on column
z=A(:,column,:);
[values,indices] = sort(z,1) ;
B=zeros(size(A,1),size(A,2),size(A,3));
for i=1:size(A,3)
B(:,:,i) =A(indices(:,:,i),:,i);
end
%% deleting row
C=B;
column=2; % delete base on column
rowSize=size(C,1);
i=1;
while true
count=0;
count1=0;
for j=1:size(C,3)
if(C(i,column,j)< 0.05)
count=count+1;
end
if(C(i,column,j)> 0.15)
count1=count1+1;
end
end
if (count==size(C,3) || count1==size(C,3))
C(i,:,:)=[];
rowSize=rowSize-1;
else
i=i+1;
end
if (i>rowSize)
break;
end
end
Updated: code with sorting and delete rows when any values in 2nd column of that row is less than 0.05 or any values in 2nd column of that row is greater than 0.15
%% deleting row
C=B;
column=2; % delete base on column
rowSize=size(C,1);
i=1;
while true
flag=0;
for j=1:size(C,3)
if(C(i,column,j)< 0.05 || C(i,column,j)> 0.15)
flag=1;
break;
end
end
if (flag==1)
C(i,:,:)=[];
rowSize=rowSize-1;
else
i=i+1;
end
if (i>rowSize)
break;
end
end

MATLAB: How to change value for linear increasing column-structure

My question is about changing values in a matrix linearly. I have a 594x1183 matrix and each cell has a value of 10. I want to change certain parts in a matrix to other values (see image below). In the solid-lined box I have a matrix with values of 10. In the dash-lined box I want to have a value of -16.
As you can see, from column 1019 to end (1183) the value should be -16. This also holds for column 1020 (to end) ... to column 1054 (to end) for the rows 54 to 182.
I can do it either manually with Excel (time-consuming) or make for every row a loop (128 loops, also time-consuming). I think there must be a quicker way to solve this problem.
So basically, for the first row (1), column 1019 to the end of matrix (column 1183) should have a value of -16 (in the first row column 1 to 1018 it has a value of 10 and from 1019 to 1183 it has a value of -16). Then the next row, the column 1020 to the end of matrix (1183) should have a value of -16 as well (in the second row, column 1 to 1019 it has a value of 10) .... repeating this to the column 1054 in row 128. So in the last row column 1 to 1053 it has a value of 10 and from 1054 to 1183 it has a value of -16.
You can make a coordinate system via meshgrid, and use that to make inequalities to use the logical indexing of arrays.
y = 594;
x=1183;
x0 = 1054;
x1 = 1019;
y0 = 54;
y1 = 182;
A = 10*ones(y,x);
[X,Y]=meshgrid(1:x,1:y);
A( Y >= y1*(X-x0)/(x1-x0) + y0*(x1-X)/(x1-x0) & Y <= y1 & Y >= y0 ) = -16;
You can check that with the spy(A) command.

how to bold text of calculated member row in crosstab crystal report?

In crosstab report of crystal report, I have
A aa 1
bb 2
cc 0
dd 1
ATotal 4
B tt 0
yy 1
hh 0
jj 1
BTotal 2
Here ATotal and BTotal are derived from the calculated member...
These are displaying as regular font as of aa,bb,cc....I want to have these ATotal and BTotal only as Bold letter, rest aa,bb,cc as regular.
Can anyone plz help me out from this?
Select the desired field and type CTRL+B.