Grouping imported data and exporting into a .txt file
조회 수: 1 (최근 30일)
이전 댓글 표시
I am suppose to import the following data from a .txt and then sort them accordingly. Those in bold are the variables, depending on the imported data. The sorting is done (>=80) - distinction, (>=50, <=79) - pass, (<=49) - file.
*56
88
32
16
99
78
44
63*
-----------------------------------------------------------------
The sample output to a .txt file is,
2 students scored a distinction. Their scores are
*88
99*
3 students scored a pass. Their scores are
*56
78
63*
3 students scored a fail. Their scores are
*32
16
44*
댓글 수: 2
채택된 답변
Cedric
2013년 10월 3일
편집: Cedric
2013년 10월 3일
Ok, the code that you provided in your comment is a good start. Here is a hint: define
>> x = [9, 3, 6, 7, 1, 2] ;
Then, for example
>> cond = x > 5
cond =
1 0 1 1 0 0
is a vector of logicals whose elements indicate where the condition is true (1) or false (0) for each element of x. A remarkable property of these vectors of logicals is that you can use them for indexing the original vector x, i.e. for getting all elements which satisfy the condition:
>> y = x(cond)
y =
9 6 7
Now you can work with y, e.g. compute its length, output it to file, etc..
Once you'll understand that, you'll have all you need to go on with this homework I guess.
댓글 수: 2
Cedric
2013년 10월 3일
For the first question:
doc length
For the second question, you cannot output an array with only one %g at the end of this formatSpec string that you are using. You have essentially two options: the first is a FOR loop over elements of vector y, which prints '%g\n' for each element. The second is a print using the same format '%g\n' directly on the y array, and use the fact that FPRINTF repeats the format as long as it "finds elements to output in the array".
I cannot say much more without giving you the answer directly. The best thing that you can do is to experiment with a small y array that you build by hand, and see whether you can understand what I said about repeated format, or try to implement a FOR loop.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Whos에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!