How to get numbers from rows in a matrix, to separate vectors?

1 次查看(过去 30 天)
I have a matrix of grades, I want to put the number of times grades repeat in each row into separate vectors so I can plot it in a scatter plot.
I have the matrix
grades=
7 7 4
12 10 10
-3 7 2
10 12 12
4 2 10
10 7 4
7 7 9
12 12 12
2 7 12
0 -3 2
4 10 2
2 12 10
10 3 4
12 12 12
I want to know how many times each number repeats in each row, and put them in a vector for each row

回答(1 个)

Samatha Aleti
Samatha Aleti 2020-1-27
According to my understanding you are trying to get the count of numbers in each row and save it in another vector. You can use “find” function to do this.
Let grades be the matrix, out be the output vector which holds the count of element “1” in each row.Here is a sample code to do this:
grades = [1 2 3; 3 2 1; 1 2 1; 1 2 3; 1 3 1;1 1 3]; % Let
numRows = size(grades,1); % Number of rows
out= zeros(1,numRows); % Initialize a vector to store the count
for i = 1:numRows
out(i) = length(find(grades(i,:) == 1));
end
Refer the following document link for detailed explanation on using "find":

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by