2.1 --> A problem with an Nx3 matrix.

조회 수: 2 (최근 30일)
Ibrahim
Ibrahim 2015년 1월 14일
편집: John D'Errico 2015년 1월 14일
Hello !
(You can read the attached file, under section: 2.1 DATA LOAD FUNCTION , thanks.)
I have written the following code:
function data = dataLoad(filename) %A function that reads a datafile and returns an nx3 matrix, with the acceptet rows. % also returning error messages. %the name of the file that i will use: tgb.csv. data = readtable('tgb.csv');
% for loop that chekcs if the temperature is between 10 og 60. if (10<(tgb(:,1)); disp('Temperature must be between 10 and 60'); elseif (tgb(:,1)<60); continue; end continue; % for loop that checks if the growthRate is a positive number. for (tgb(:,2) < 0); disp('Growth rate must be postive'); continue; end % for loop that identifies the bacteria. for (tgb(:,3) > 4) error('invalid number. The number must be between 1 and 4'); case tgb(:,3) of 1 do return('Salmonella enterica'); of 2 do return('Bacillus cereus'); of 3 do return('Listeria'); of 4 do return('Brochothrix thermosphacta'); continue; end
----------------------------------------------------------------------------------------
That does not Work for me! First this is my 7 matlab problem, and i am new user... So please be patient :-)
I have to figure out: 1. how to call the matrix, and display the rows that are "true" in a N x 3 matrix. 2. how to make the conditions for ROWS instead of COLOMNS. (That is what i have done) --> Is it something like:
for i = 1:length(data(:,1))-1 % loop gennem alle rækker. if sum(data(i,:))>sum(data(i+1,:)); end end elseif CONDITION
elseif CONDITION
end ???
???
If someone could help me find an easier way or help me with the solution, it will be fantastic,
NOTICE: I only need help with the 2.1 section in the attached file. I have also attached my csv file.
Thanks for your time!
  댓글 수: 1
Ibrahim
Ibrahim 2015년 1월 14일
All right, i did not figure it out completly, but I got another question for you. Let us say we identify the rows that are not living up to the condition. Which Means the row must be deleted. How do you delete a row in matlab.
fx a matrix; [1,2,3:4,5,6:7,8,9;1,2,3;2,3,4]; And we have to remove all the rows that have one of the values greater than 4. Which Means that we have to remove the second and third row. But how do you do that?

댓글을 달려면 로그인하십시오.

채택된 답변

John D'Errico
John D'Errico 2015년 1월 14일
편집: John D'Errico 2015년 1월 14일
Deleting rows or columns is done by an index operation. Set that row (or column) or rows, etc., to []. For example:
A = magic(4)
A =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
Lets delete the first and third rows.
A([1 3],:) = []
A =
5 11 10 8
4 14 15 1
As you can see, only the 2nd and 4th rows remain. Column deletion works the same way, by column indexing, setting them to empty.
As for your other questions, I have no idea what you mean by how to "call the matrix". I might try, "Here matrix, here boy". It works for my dog anyway.
I also do not know exactly what you mean by "Displaying the rows that are true". To find any row of M that has any value that exceeds 4, just do a test.
A = [1,2,3:4,5,6:7,8,9;1,2,3;2,3,4];
rowind = any(A > 4,2)
rowind =
0
1
1
0
0
As you can see, this returns a boolean vector, true or false for any rows that met the criterion.
Now we can zap those rows away via one of two methods. We can use find to find the index of the rows for deletion, or we can just index directly using the boolean vector. So either of these following forms will work:
A(find(rowind),:) = [];
A(rowind,:) = [];
Sometimes you will need to use the actual indices of those rows for other purposes, so you might use the find operation. Either case will result in the matrix:
A
A =
1 2 3
1 2 3
2 3 4
Finally, I would point out that nothing I did here used or needed an explicit loop. This is MATLAB. Learn to use matrices and vectors.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by