Calling elements of a row vector in Loop function
13 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi, I have the following code where I want to call elements of vector z to in the loop environment. However, i am unable to save the results in each iteration and print it accordingly. My code is as follows;
z = 0:0.01:0.1; % Create a sequence
b =[10;20]; % column vector
x=zeros(1,numel(z)); % preallocate for element of interest
A=zeros (1,numel(z)); % preallocate for element of result
invA=zeros (1,numel(z)); % preallocate for element of result
for k=1:numel(z)
A(k) = [2+z(k) 2.5; 2.5 3+z(k)] ; % matrix of 2*2 with elements depend on value of elements called from z vector
invA(k) = inv(A(k));
x(k) = invA(k)*b;
end
I am getting the following error, "Unable to perform assignment because the left and right sides have a different number of elements." I dont know how to resolve this issue in this context. I require the print of all these three elements of interest along with corresponding z values used. Any help is appreciated.
2 commentaires
Stephen23
le 1 Mar 2021
"x = A\b is computed differently than x = inv(A)*b and is recommended for solving systems of linear equations."
You should use mldivide (as shown) because it is more efficient and numerically more robust.
Réponses (1)
KALYAN ACHARJYA
le 1 Mar 2021
Modifié(e) : KALYAN ACHARJYA
le 1 Mar 2021
To save the array or matrix data use cell array. The ( ) allows to save the scalar numeric data only.
{ } bracket
Read about cell array
More:
x=cell(1,numel(z));
A=cell(1,numel(z));
invA=cell(1,numel(z));
for k=1:numel(z)
A{k}=.....
invA{k}=inv(A{k});
x{k}=invA{k}*b;
end
See the cell2mat function also.
Note: Access the array elements use () bracket in Numeric array data and access cell array data ements using { } bracket in cell array data. Cell array allows all save all type of data in its data elements (Including file also, audio, video etc)
Hope it Helps!
4 commentaires
KALYAN ACHARJYA
le 1 Mar 2021
"how to concatenate z, which is numeric vector, and A, invA , which are cells vectors where each cell is of 2 * 2 dimension. and x which is cells vector where each cell is 2 * 1 matrix."
If you want to concatanete a numeric array and cell. Initially you need to perform cell to matrix (cell2mat) on cell data. There are plenty of options for arranging cell data elements in the resulting matrix, please see the MATLAB docs for cell2mat function. Once you get two numeric arrays, you can concatenate them horizontally, if both have same numbers rows and vertically, if both have same number of columns.
result=[A,B], %if Both have same number of rows
result=[A;B]; %if both have same number of columns
"Secondly, how can i retrieve first element of each cell in x cells row vector and save it to some numeric or cell vector separately. i.e. suppose x is cells row vector where each cell is combination of 2 by 1 column vectors in each cell."
1D Cell Array
first_element=cell{1}
2D Cell Array
first_element=cell{1,1}
It is about data arrangement for better displaying purpose, you can do this in many ways (if you are properly understanding data handling in cell array).
Voir également
Catégories
En savoir plus sur Data Type Conversion dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!