How to access element of a matrix using vector function
조회 수: 10 (최근 30일)
이전 댓글 표시
lets say if A is a matrix 3x10 and A(:,i) will return a column vector of 3x1.
How I could use A(:,i) to get the specific element for eg 1st element (1x1) or 2nd element (2x1) of that column matrix extracted ?
Any leads would be much appreciated.
A=rand(3,10)
A(:,3)
A(:,3)(1)
댓글 수: 0
채택된 답변
Dyuman Joshi
2023년 7월 5일
Use indexing -
A=rand(3,10)
A(:,3)
%First element
A(1,3)
%First two elements
A(1:2,3)
%Last two elements
A(2:3,3)
추가 답변 (3개)
Aman
2023년 7월 5일
Hi Shubhankar,
You can use of the following two approaches:
- Store the output in a new variable.
A=rand(3,10)
i = 2;
columnVector = A(:, i);
% Access specific elements of the column vector
element1 = columnVector(1); % 1st element
element2 = columnVector(2); % 2nd element
2. Access the element directly.
A=rand(3,10)
% Access specific elements of the i-th column vector
i = 2;
element1 = A(1, i); % 1st element
element2 = A(2, i); % 2nd element
Hope this helps!
Shubham Dhanda
2023년 7월 5일
Hi,
I understand that you are not able extract a specific element of column Vector using the given indexing. An easy approach would be intially extracting a column vector and then use indexing on the column vector to extract the specific element.
A = rand(3, 10);
columnVector = A(:, 3);
element = columnVector(1);
disp(element);
Note : There can be a lot of ways to access the element
Aakash
2023년 7월 5일
Hi,
1.So one way is you can access the specific row in a column vector through a seperate line like;
column_vector = A(:, 3); % Extract the column vector at index 3
element_1 = column_vector(1); % Access the first element of the column vector
element_2 = column_vector(2); % Access the second element of the column vector
or
2. You can access element at row x, column y through A(x,y)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!