Calling elements of a row vector in Loop function
Mostrar comentarios más antiguos
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.
Respuestas (1)
KALYAN ACHARJYA
el 1 de Mzo. de 2021
Editada: KALYAN ACHARJYA
el 1 de Mzo. de 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 comentarios
Stephen23
el 1 de Mzo. de 2021
"The ( ) allows to save the scalar numeric data only. "
Or cell arrays, or structure arrays, or string arrays, or tables, ...
"Access the array elements use () bracket in Numeric array data and access cell array data ements using { } bracket in cell array data."
For all fundamental types parentheses always refer to the elements of the array itself.
For container types (cell, table, string) curly braces refer to the content of the array.
KALYAN ACHARJYA
el 1 de Mzo. de 2021
@Stephen Cobeldick Thanks sir for the explanation.
Muhammad Arslan Iqbal
el 1 de Mzo. de 2021
KALYAN ACHARJYA
el 1 de Mzo. de 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).
Categorías
Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!