for-loop to create vector
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
I am just learning for end loops.
I am trying to create a for end loops that can find the maximum value of each column of a matrix and store the results in a single vector.
I have ankle moment data and I want to identify the maximum moment of each stride (column in the data) using a for-loop to store the results in a vector.
채택된 답변
Ameer Hamza
2020년 10월 15일
편집: Ameer Hamza
2020년 10월 15일
0 개 추천
Since you insist on using for-loop
M; % your matrix
n = size(M, 2); % number of columns
max_vals = zeros(1, n);
for i = 1:n
max_vals(i) = max(M(:,i));
end
Without for-loops
max_vals = max(M);
댓글 수: 15
Slane Haltine
2020년 10월 15일
편집: Slane Haltine
2020년 10월 15일
The code seems to work but I recieve an output of max_vals which is only one number, which is the maximum value. As opposed to a vector containing the max values of each column.
n = size(Force,10);
max_vals = zeros(1,n);
for i = 1:n
max_vals(i) = max(Force(:,i));
end
Rik
2020년 10월 15일
Have you read the documentation for the size function? You're asking for the tenth dimensions. I doubt you have that many.
Slane Haltine
2020년 10월 15일
편집: Slane Haltine
2020년 10월 15일
Okay, I was confused by the original code stating %number of columns
Thank you, I have read it but was misinterpretting it before.
In that case it absolutely works but how can I have the output as a column vector instead of a a row?
Ameer Hamza
2020년 10월 15일
If you want output to be in column vector form then initialize like this
max_vals = zeros(n,1);
Slane Haltine
2020년 10월 15일
Thank you very much!
Ameer Hamza
2020년 10월 15일
I am glad to be of help!
Slane Haltine
2020년 10월 15일
I am a bit confused about the use of zeros in this line of code. The code works perfectly, but can you elaborate as to what this line is calling for?
Ameer Hamza
2020년 10월 15일
Read about pre-allocation in MATLAB. It is just there to make the code more efficient. Removing it will not cause any error but the code can get very slow if the array needs to be large.
Rik
2020년 10월 15일
Have you read the documentation? It is one of the major benefits of Matlab over competing software.
Slane Haltine
2020년 10월 15일
편집: Slane Haltine
2020년 10월 15일
I think I understand and I see how it can make the code much more efficient for a much larger array. If i remove it, the values are given in a row vector again.
This is what confuses me as to its function in this code.
for example, without it, this is how I would write the code.
n = size(Force,2);
max_vals = 0;
for i = 1:n
max_vals(i) = max(Force(:,i));
end
max_column = reshape(max_vals,10,1)
Rik
2020년 10월 15일
You can make it a row vector if you prefer. It is good to form the habit when you learn, so you don't need to unlearn bad habits later.
Slane Haltine
2020년 10월 15일
I mean to say, I added the reshape line at the end of the code because removing the zeros line results in a row instead of a column. That confuses me as to what the zeros command is doing in that line of code and how it makes the difference between the max_vals resulting in a row or column vector.
Rik
2020년 10월 15일
If you don't specify two coordinates, Matlab will attempt to figure out which dimensions should be extended to make your data fit. The default is to grow in the first dimension. If you create the array with a specific shape, Matlab will extend that. So you can choose between zeros(1,n); or zeros(n,1); (or more exotic shapes like zeros(1,1,n);).
Slane Haltine
2020년 10월 15일
Awesome, thank you!
Ameer Hamza
2020년 10월 16일
Note that If you just want to conver a row vector to a column vector then you can even use
max_column = max_column(:);
% or
max_column = max_column.'; % in this case, max_column must be a row vector
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
