Calling elements of a row vector in Loop function

조회 수: 1 (최근 30일)
Muhammad Arslan Iqbal
Muhammad Arslan Iqbal 2021년 3월 1일
댓글: Muhammad Arslan Iqbal 2021년 3월 7일
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
Stephen23
Stephen23 2021년 3월 1일
Note that inv should be avoided. The inv documentation states:
"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.
Muhammad Arslan Iqbal
Muhammad Arslan Iqbal 2021년 3월 7일
@Stephen Cobeldick thank you for the comment. Noted.

댓글을 달려면 로그인하십시오.

답변 (1개)

KALYAN ACHARJYA
KALYAN ACHARJYA 2021년 3월 1일
편집: KALYAN ACHARJYA 2021년 3월 1일
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
Muhammad Arslan Iqbal
Muhammad Arslan Iqbal 2021년 3월 1일
@KALYAN ACHARJYA and @Stephen Cobeldick thank you so much for the support and suggestions. However, i am struggling with the second part of my question. 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. The reason i want to concatenate is to have a table as an output to see how changing in value of z results in change in A, inv(A), and x.
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.
KALYAN ACHARJYA
KALYAN ACHARJYA 2021년 3월 1일
"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).

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by