필터 지우기
필터 지우기

Finding the first and last element of a matrix

조회 수: 151 (최근 30일)
Sobhan
Sobhan 2012년 5월 30일
댓글: Thabo Alfred Tsebe 2021년 11월 19일
Dear all,
I have a problem and I hope somebody can help.
I want to find the value of min, max, start and end of different columns (rows 1:50) of my matrix in a loop.
the formula below gave me the min and max as I wanted:
f = 1:nfiles
output1 (:,f)= min (mydata (1:50 (:,f),f));
otput2 (:,f)= max (mydata (1:50 (:,f),f));
Now how can I find the first and last element? So in my case the 1 and 50?
I hope I am clear enough.
Thanks in advance,
Sobhan

답변 (2개)

Thomas
Thomas 2012년 5월 30일
Suppose you have a matrix of 5 columns by 50 rows
val=rand(50,5); % generate the data
min_val=min(val) % min value in each column
max_val=max(val) % max value in each column
first_ele=val(1,:) % first value
last_ele=val(50,:) %last value (50th)
edit
IF you donot know the position of last element use
last_ele=val(end,:) %last value
  댓글 수: 2
Sobhan
Sobhan 2012년 5월 30일
Dear Thomas,
Thanks for your answer. I forgot to say something: the rows that I am interested in (in my example 1:50) changes throughout the loop. For example for one file is 1:30 and for another one is 1:15.
I want a kind of solution that find the first and last element regardless my indexing range. Finding the min and max was easy but first and last element no!
cheers
Thomas
Thomas 2012년 5월 30일
check my edit..

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


Nurye Hassen
Nurye Hassen 2017년 11월 23일
The first element is obvious.i.e. A(1,1) gives you the first elemnt of A. But for the last one, When ever the loop changes you can ask the new number of the row and its respective value. Here is an example..Inside the loop write... For matrix A
rows=size(A,1); Value=A(rows,end);

카테고리

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