Effacer les filtres
Effacer les filtres

How to access element of a matrix using vector function

13 vues (au cours des 30 derniers jours)
Shubhankar
Shubhankar le 5 Juil 2023
Commenté : Shubhankar le 5 Juil 2023
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.

Réponse acceptée

Dyuman Joshi
Dyuman Joshi le 5 Juil 2023
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
  2 commentaires
Shubhankar
Shubhankar le 5 Juil 2023
Hi Dyuman Joshi,
Thanks so much for your quick response, your solution worked for me.
Regards,
Shubhankar
Dyuman Joshi
Dyuman Joshi le 5 Juil 2023
Glad to have helped!

Connectez-vous pour commenter.

Plus de réponses (3)

Aman
Aman le 5 Juil 2023
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!
  1 commentaire
Shubhankar
Shubhankar le 5 Juil 2023
Hi Aman,
Thanks so much for your quick response, your solution worked for me.
Regards,
Shubhankar

Connectez-vous pour commenter.


Shubham Dhanda
Shubham Dhanda le 5 Juil 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
  1 commentaire
Shubhankar
Shubhankar le 5 Juil 2023
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

Connectez-vous pour commenter.


Aakash
Aakash le 5 Juil 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)
  1 commentaire
Shubhankar
Shubhankar le 5 Juil 2023
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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by