MATLAB Answers

Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 1-by-0.

3 views (last 30 days)
Rahul Gulia
Rahul Gulia on 8 Sep 2020
Commented: Rahul Gulia on 9 Sep 2020
This is what i am trying to solve.
>> d = 1 2 3 4 5 6 7 8 9 10
>> dref = 3 5 7
>> A = % Creating from d of size (10x10) using (A = repmat(d, length(d),1))
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
>> B = % Taking only the lower traingle values from the diagonal.
1 0 0 0 0 0 0 0 0 0
1 2 0 0 0 0 0 0 0 0
1 2 3 0 0 0 0 0 0 0
1 2 3 4 0 0 0 0 0 0
1 2 3 4 5 0 0 0 0 0
1 2 3 4 5 6 0 0 0 0
1 2 3 4 5 6 7 0 0 0
1 2 3 4 5 6 7 8 0 0
1 2 3 4 5 6 7 8 9 0
1 2 3 4 5 6 7 8 9 10
using this;
B = zeros(length(d),length(d));
for i = 1:length(d)
B(i,1:i) = d(1):d(i);
end
* This is what i have already done. Now i want two matrices out of "B" matrix.
matrix C = containing all the diagonal values of B, and the values of "dref" in each row.
example, C =
1 0 0 0 0 0 0 0 0 0
0 2 0 0 0 0 0 0 0 0
0 0 3 0 0 0 0 0 0 0
0 0 3 4 0 0 0 0 0 0
0 0 3 0 5 0 0 0 0 0
0 0 3 0 5 6 0 0 0 0
0 0 3 0 5 0 7 0 0 0
0 0 3 0 5 0 7 8 0 0
0 0 3 0 5 0 7 0 9 0
0 0 3 0 5 0 7 0 0 10
matrix D = containing all the values of matrix B, without the diagonal elements and the "dref" values in each row.
example, D =
0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
1 2 0 0 0 0 0 0 0 0
1 2 0 0 0 0 0 0 0 0
1 2 0 4 0 0 0 0 0 0
1 2 0 4 0 0 0 0 0 0
1 2 0 4 0 6 0 0 0 0
1 2 0 4 0 6 0 0 0 0
1 2 0 4 0 6 0 8 0 0
1 2 0 4 0 6 0 8 9 0
I have tried every method in my mind to solve it. But of no use. Just a hint would also suffice guys.
Thanks in advance.

  4 Comments

Show 1 older comment
Walter Roberson
Walter Roberson on 8 Sep 2020
Look at your first value in dlink, which is 0. You intersect(0, dref) where dref is [7 3 6 6]. The intersection of those two sets is empty. You try to assign that emptiness to the definite location A(ii,p) . That is a mismatch on sizes, no values to assign but has to go into a place that has holds exactly one value.
I am trying to find the dref values in each outer loop
I do not understand that?
Are you trying to find the dlink values that are members of dref? If so then without loops,
intersect(dlink, dref)
which would give you [3 5 7]. That would not appear to be useful, as it is going to be the same as unique(dref)

Sign in to comment.

Accepted Answer

Rik
Rik on 9 Sep 2020
This should do the trick:
%your inital code:
d = 1:10;
dref = [3 5 7];
A = repmat(d, length(d),1);
B = zeros(length(d),length(d));
for i = 1:length(d)
B(i,1:i) = d(1):d(i);
end
%my code:
d = 1:10;
dref = [3 5 7];
A=repmat(reshape(d,1,[]),numel(d),1);
B=tril(A);
diagonal=B.*(eye(size(B)));
lower_part=B.*(1-eye(size(B)));
L=ismember(lower_part,dref);
lower_part(~L)=0;
C=lower_part+diagonal;
D=B-C;

More Answers (0)

Community Treasure Hunt

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

Start Hunting!