Speadsheet with all the answers from my loop

조회 수: 1 (최근 30일)
Jordan Grapel
Jordan Grapel 2019년 10월 14일
댓글: Jordan Grapel 2019년 10월 14일
I created a for loop that was meant to show me how many times a number showed up in an array like so:
for i=1:n
length(find(yy(1:numel(yy),1)==i))
end
It worked but it did so in a way that gave me eac number's answer one at a time in my command window like so:
ans=
7
ans=
2
and so on.
How do I code it so that I create a speadsheet that tells me the answer from 1 all the way to the last number?
  댓글 수: 2
Geoff Hayes
Geoff Hayes 2019년 10월 14일
Jordan - what are you trying to write to file? A list of the integers and the number of times they appear in the array?
Jordan Grapel
Jordan Grapel 2019년 10월 14일
That's pretty much what the loop does. The numbers are already in numeric order in a different spreadsheet. This loop tells me how many times each number appears. For example, if 1 appears once, 2 appears 4 times, and 3 appears 18 times the loop produces:
ans=
1
ans=
4
ans=
18
and so on in the command window all the way until the last number (which for this file is 518). My problem is that I am only getting this long run of answers in my command window. I need to make it so that all of these answers (number of times the number appears in the array) appears on a spreadsheet with them all appearing in column one, and with the answer for 1 appearing in row one and so on.

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

채택된 답변

the cyclist
the cyclist 2019년 10월 14일
편집: the cyclist 2019년 10월 14일
output = zeros(n,1);
for i=1:n
output(i) = length(find(yy(1:numel(yy),1)==i));
end
output
That will still write the full list to the screen, but without the intervening "ans" text.
There are then a number of ways to write to file. Perhaps check out this documentation as a starting point.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by