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×10
0.3244 0.1814 0.6406 0.9759 0.4976 0.8527 0.2886 0.1458 0.1342 0.7906 0.5563 0.8865 0.4226 0.9473 0.7795 0.7904 0.8093 0.4554 0.5173 0.2380 0.7484 0.8836 0.5552 0.3947 0.2538 0.8797 0.2439 0.1801 0.1451 0.0432
A(:,3)
ans = 3×1
0.6406 0.4226 0.5552
A(:,3)(1)
Invalid array indexing.

采纳的回答

Dyuman Joshi
Dyuman Joshi 2023-7-5
Use indexing -
A=rand(3,10)
A = 3×10
0.4931 0.4688 0.5949 0.5627 0.8420 0.4994 0.3968 0.3644 0.0634 0.5203 0.5138 0.8816 0.1320 0.4975 0.5256 0.0801 0.6158 0.1977 0.2617 0.4064 0.1271 0.6517 0.2315 0.4487 0.5137 0.6693 0.3537 0.6661 0.8979 0.0736
A(:,3)
ans = 3×1
0.5949 0.1320 0.2315
%First element
A(1,3)
ans = 0.5949
%First two elements
A(1:2,3)
ans = 2×1
0.5949 0.1320
%Last two elements
A(2:3,3)
ans = 2×1
0.1320 0.2315

更多回答(3 个)

Aman
Aman 2023-7-5
Hi Shubhankar,
You can use of the following two approaches:
  1. 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
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
  1 个评论
Shubhankar
Shubhankar 2023-7-5
Thanks so much for your quick response, your solution worked might work for me but i dont want to save the element in new variable but approach is working fine for me.
Hvave a great day ahead.
Regards,
Shubhankar

请先登录,再进行评论。


Aakash
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)
  1 个评论
Shubhankar
Shubhankar 2023-7-5
Hi Aakash,
Thanks so much for your quick response, your solution worked might work for me but i dont want to save the element in new variable but approach is working fine for me.
Have a great day ahead.
Regards,
Shubhankar

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by