"Subscripted assignment dimension mismatch" Please correct my program. tell me idea/logic to make sequence

s = [30 30 30 30 30 30 0 0 0 0];
s = sort(s, 'descend'); %sorting sequence s
uniques = unique(s); % finding unique elements
for k = 1: length(uniques)
ua(:,:,k) = repmat(uniques(:,k),1 ,(length(s))); %repeating unique values for combine
end
for k= 2:length(s)
for j = length(s)-2:-1:1
for l = 1:6
ua1(:,:,1) = [ua(1, 1:j, 1), ua(1, 1:k, 2)]; %cut and combine values
end
end
end
output ua1
ua1 =
0 0 0 0 0 0 0 0 30 30
my loop for ua1 is not working. please help me.
I want to generate sequence
ua1(:,:,1) = [0 0 0 0 0 0 0 0 30 30];
ua1(:,:,2) = [0 0 0 0 0 0 0 30 30 30];
ua1(:,:,3) = [0 0 0 0 0 0 30 30 30 30];
ua1(:,:,4) = [0 0 0 0 0 30 30 30 30 30];
ua1(:,:,5) = [0 0 0 0 30 30 30 30 30 30];
ua1(:,:,6) = [0 0 0 30 30 30 30 30 30 30];
or if possible tell me trick to make
[0 0 0 0 0 0 0 0 0 30;
0 0 0 0 0 0 0 0 30 30;
0 0 0 0 0 0 0 30 30 30;
0 0 0 0 0 0 30 30 30 30;
0 0 0 0 0 30 30 30 30 30;
0 0 0 0 30 30 30 30 30 30;
0 0 0 30 30 30 30 30 30 30;
0 0 30 30 30 30 30 30 30 30;
0 30 30 30 30 30 30 30 30 30];

 Accepted Answer

Don't waste time with all of those loops when you can simply use hankel to generate the whole matrix:
>> hankel(zeros(1,9),[0,30*ones(1,9)])
ans =
0 0 0 0 0 0 0 0 0 30
0 0 0 0 0 0 0 0 30 30
0 0 0 0 0 0 0 30 30 30
0 0 0 0 0 0 30 30 30 30
0 0 0 0 0 30 30 30 30 30
0 0 0 0 30 30 30 30 30 30
0 0 0 30 30 30 30 30 30 30
0 0 30 30 30 30 30 30 30 30
0 30 30 30 30 30 30 30 30 30

6 Comments

can you make it into loop "ua1"
s = [30 30 30 30 30 30 0 0 0 0];
s = sort(s, 'descend'); %sorting sequence s
uniques = unique(s); % finding unique elements
for k = 1: length(uniques)
ua(:,:,k) = repmat(uniques(:,k),1 ,(length(s))); %repeating unique values for combine
end
ua1(:,:,1) = [ua(1, 1:8, 1), ua(1, 9:10, 2)];%cut and combine values
ua1(:,:,2) = [ua(1, 1:7, 1), ua(1, 8:10, 2)];
ua1(:,:,3) = [ua(1, 1:6, 1), ua(1, 7:10, 2)];
ua1(:,:,4) = [ua(1, 1:5, 1), ua(1, 6:10, 2)];
ua1(:,:,5) = [ua(1, 1:4, 1), ua(1, 5:10, 2)];
ua1(:,:,6) = [ua(1, 1:3, 1), ua(1, 4:10, 2)];
ua1(:,:,7) = [ua(1, 1:2, 1), ua(1, 3:10, 2)];
I do not understand what you want. You are asking me about some other code that has nothing to do with my answer. I certainly would not try to solve this with loops. That is why I gave you a very simple answer that generates the "sequence" that you claimed you wanted. If you want it rearranged into a 3D array, then try something this:
>> X = hankel(zeros(1,9),[0,30*ones(1,9)]);
>> permute(X,[3,2,1])
ans(:,:,1) =
0 0 0 0 0 0 0 0 0 30
ans(:,:,2) =
0 0 0 0 0 0 0 0 30 30
ans(:,:,3) =
0 0 0 0 0 0 0 30 30 30
ans(:,:,4) =
0 0 0 0 0 0 30 30 30 30
ans(:,:,5) =
0 0 0 0 0 30 30 30 30 30
ans(:,:,6) =
0 0 0 0 30 30 30 30 30 30
ans(:,:,7) =
0 0 0 30 30 30 30 30 30 30
ans(:,:,8) =
0 0 30 30 30 30 30 30 30 30
ans(:,:,9) =
0 30 30 30 30 30 30 30 30 30
That covers both of the outputs that you claim that you want.
Thanks stephen.... but if i say you for
[60 60 60 60 60 60 60 60 60 30;
60 60 60 60 60 60 60 60 30 30;
60 60 60 60 60 60 60 30 30 30;
60 60 60 60 60 60 30 30 30 30;
60 60 60 60 60 30 30 30 30 30;
60 60 60 60 30 30 30 30 30 30;
60 60 60 30 30 30 30 30 30 30;
60 60 30 30 30 30 30 30 30 30;
60 30 30 30 30 30 30 30 30 30];
Means different type of theta....with non zero element then???
It seemed rather straightforward, to me at least, that all we have to do is to replace zeros in Stephen's code with 60's. So I simply did this:
output = hankel(60*ones(1,9),[0,30*ones(1,9)])
and voila! Exactly what you asked for. It's so easy that I'm sure you must have thought it already, so what happened when you tried it? Did you get an error or something? Paste all the red text back here so we can see how your code differs from mine.

Sign in to comment.

More Answers (1)

ua1(:,:,1) = [ua(1, 1:j, 1), ua(1, 1:k, 2)]; %cut and combine values
In that code, 1:j will have one length for the first j, but for the next j would have a different length. Therefore for different iterations of j, the right hand side will be different sizes, but each time you try to write it to the same size on the left.

1 Comment

1:j is in decreasing order....and 1:k is in increasing order...size of ua1 matrix same for every iteration. Sir if you have any idea ...please correct this or make any program with your blessings..for generating these sequnces.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Asked:

on 22 Feb 2016

Commented:

on 23 Feb 2016

Community Treasure Hunt

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

Start Hunting!