How to deal with dimension of the both sides?

1 visualizzazione (ultimi 30 giorni)
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 Commenti
Nikhil Sapre
Nikhil Sapre il 2 Lug 2021
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 il 2 Lug 2021
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.

Accedi per commentare.

Risposta accettata

DGM
DGM il 2 Lug 2021
Modificato: DGM il 2 Lug 2021
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

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Prodotti


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by