How to access element of a matrix using vector function
10 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Dyuman Joshi
on 5 Jul 2023
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)
2 Comments
More Answers (3)
Aman
on 5 Jul 2023
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
on 5 Jul 2023
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
on 5 Jul 2023
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)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!