Why this gives error?

LB=[0 0 0 0];
UB=[10 10 pi pi];
particles_x(1,1)=LB+rand*(UB-LB)
Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.

1 件のコメント

Jan
Jan 2022 年 4 月 15 日
I've added the error message. Please care for including the message in future questions about errors.

サインインしてコメントする。

回答 (2 件)

Jan
Jan 2022 年 4 月 15 日

0 投票

LB = [0 0 0 0];
UB = [10 10 pi pi];
size(LB + rand * (UB - LB))
ans = 1×2
1 4
You cannot assign a [1 x 4] vector to the scalar particles_x(1,1).
KuriGohan
KuriGohan 2022 年 4 月 15 日

0 投票

You get a 1x4 array on the right that you want to save inside the first row of the first column of the array - you cant do that. Just write particles_x=LB+rand*(UB-LB) and then particles_x(1,1) to acess what you want (at least what i think you want, that is the first element of that matrix).

5 件のコメント

Sadiq Akbar
Sadiq Akbar 2022 年 4 月 15 日
Thanks to both of you Jan and KuriGohan for your prompt response. But actually, This is inside an algorithm and when I change it like this:
LB=[0 0 0 0];
UB=[10 10 pi pi];
particles_x(1,:)=LB+rand*(UB-LB)
Then it doesn't give error but then I get -ve answers also. So what to do so that I don't get -ve answers?
KuriGohan
KuriGohan 2022 年 4 月 15 日
Acess just what you want - in your case:
LB=[0 0 0 0];
UB=[10 10 pi pi];
particles_x(1,:)=LB+rand*(UB-LB);
particles_x = particles_x(1,1)
Sadiq Akbar
Sadiq Akbar 2022 年 4 月 15 日
Thank you very much dear KuriGohan for your prompt response.Actually the code of the algorithm is at: https://www.mathworks.com/matlabcentral/fileexchange/67768-a-hybrid-firefly-and-particle-swarm-optimization-hfpso?s_tid=FX_rc2_behav
If you download it, it works for scalar bounds but gives an error of dimension mismatch. So when I do changes as mentioned above, then i get -ve values in results also which is not required.
Jan
Jan 2022 年 4 月 16 日
What are "-ve values" ?
Voss
Voss 2022 年 4 月 16 日
I gather "-ve" means "negative" (so the "-" stands for "negati", I guess).

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

質問済み:

2022 年 4 月 15 日

コメント済み:

2022 年 4 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by