How to deal with dimension of the both sides?

조회 수: 1 (최근 30일)
vimal kumar chawda
vimal kumar chawda 2021년 7월 2일
편집: DGM 2021년 7월 2일
Unable to perform assignment because the indices on the left side are not compatible with the
size of the right side.
X1_bar_i =[9.59547499999917
58282.181075
35.4837749999997
58288.903275
-35.4750250000006
58455.670975
-9.60422500000095
58462.389975]
X2_bar_i =[9.59557499999937
58282.183075
35.4903749999994
58288.908875
-35.4809250000008
58455.676575
-9.60502500000075
58462.394475]
for i=1:size(X1_bar_i)
H(i,1)= [0 X1_bar_i(i+1,1) X1_bar_i(i,1) -X1_bar_i(i+1,1) 0 1;X1_bar_i(i+1,1) X1_bar_i(i,1) 0 X1_bar_i(i,1) 1 0];
i=i+1;
end
The dimension of the H should be 2*6. How can I do it?
  댓글 수: 2
Nikhil Sapre
Nikhil Sapre 2021년 7월 2일
Can you explain what do you mean by the dimension of the H should be 2*6? Why?
How is H formed? Can you elaborate with a simple example?
Scott MacKenzie
Scott MacKenzie 2021년 7월 2일
In MATLAB, the loop index variable in a for-loop cannot be changed via code in the loop. So, the line
i = i + 1;
has no bearing on the value of the variable i as used in the loop.
Other issues... X1_bar_i is a vector, not a matrix. Accessing elements require one index, not two.
Also, accessing the element in position i+1 of X1_bar_i is not possible in the last iteration when i is equal to the length of the vector. That is an attempt to access the element past the end of the vector, which is not possible.
So, there are a few issues to think about here. Good luck.

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

채택된 답변

DGM
DGM 2021년 7월 2일
편집: DGM 2021년 7월 2일
Start here
X1_bar_i =[9.59547499999917
58282.181075
35.4837749999997
58288.903275
-35.4750250000006
58455.670975
-9.60422500000095
58462.389975]
X2_bar_i =[9.59557499999937
58282.183075
35.4903749999994
58288.908875
-35.4809250000008
58455.676575
-9.60502500000075
58462.394475]
% preallocate.
% since each iteration calculates a 2D array, i'm going to stack them on dim3
% if you want them edge-concatenated, you'll have to make that change.
H = zeros(2,6,size(X1_bar_i,1)-1);
% size(X1...) is [8 1]. pick one dimension explicitly, or use numel()
% since array indexing offsets +1, i can't extend to the end
% don't know if that's what you were hoping for
% for i=1:size(X1_bar_i)
for i = 1:size(X1_bar_i,1)-1
H(:,:,i) = [0 X1_bar_i(i+1) X1_bar_i(i) -X1_bar_i(i+1) 0 1;
X1_bar_i(i+1) X1_bar_i(i) 0 X1_bar_i(i) 1 0];
% i=i+1; % this doesn't do anything. don't set the loop iterator inside the loop
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by