Unable to perform assignment because the left and right sides have a different number of elements

1 view (last 30 days)
I have the follwing matlab code. After excecuting it, error is displayed as "Unable to perform assignment because the left and right sides have a different number of elements." Kindly help me out.
k12=200;
k34=500;
K=zeros(4,4);
a=2:0.02:9;
b=1:length(a);
kc(b)=2:0.02:9;
for b=1:length(a)
kc(b)=2:0.02:9;
K(b)=[k12 -k12 0 0;-k12 kc(b)+k12 -kc(b) 0;0 -kc(b) k34+kc(b) -k34;0 0 -k34 k34];
end

Answers (1)

Image Analyst
Image Analyst on 2 Nov 2021
kc(b) is a single element because b is a single number between 1 and length(a).
2:0.02:9 is a 351 long row vector, NOT a single number.
You cannot stuff 351 numbers into an element meant to take only one number.
K(b) is a single element because b is a single number between 1 and length(a).
[k12 -k12 0 0;-k12 kc(b)+k12 -kc(b) 0;0 -kc(b) k34+kc(b) -k34;0 0 -k34 k34]; is a 4-by-4 matrix, NOT a single number.
You cannot stuff a 4x4 matrix into an element meant to take only one number.
  6 Comments
venkatesu u
venkatesu u on 4 Nov 2021
Hi
I need to work with all of the values. I tried in another way with the following code.
clear all
clc
kc=[1 2 3 4 5 6 7 8 9 0];
k12=200;
k34=300;
new1=1:1:10;
for h1=1:length(new1);
for l1=1:length(new1);
K(h1,l1)=[k12 -k12 0 0;-k12 kc(h1,l1)+k12 -kc(h1,l1) 0;0 -kc(h1,l1) k34+kc(h1,l1) -k34;0 0 -k34 k34];
end
end
But it shows the following error after execution in MATLAB.
"Unable to perform assignment because the indices on the left side are not compatible with the size of the right side"
Kindly help me with this.
Image Analyst
Image Analyst on 4 Nov 2021
Same problem and same question. You're trying to stick a 4x4 matrix into a location that can hold only one value. I don't know why you are trying to do that other than you saying you think you need to. But it's not allowed.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!