Error using horzcat Dimensions of arrays being concatenated are not consistent
Show older comments
Dear,
I have a program but When I run it, an error occurs:
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
Error in (line 34)
H1_modified = [H1 extra_cols];
How can I fix this error please ?
5 Comments
As the error clearly states, the sizes of variables H1 and extra_cols are not compatible for horizontal concatenation.
What exactly do you want to do with that particular operations of horizontal and vertical concatenation? What is the expected output from that step?
Also, note that m=1 and m=2 will lead to an error in the 3rd and 5th lines of the function get_random_index(), respecitvely.
m=4;%input('Enter the number of elements m = ');
Hb=hankel(1:m,m:-1:1); % Hankel matrix
%% Initialization of the sub-matrices with dimensions (m-1 x m-1)
a=zeros(m-1,m-1,m-2);
a(:,:,1)=eye(m-1); % the identity sub-matrix with index = 1
for k=2:m-1
a(:,:,k)=circshift(a(:,:,k-1),1,2); % Sub-matrix with indexes = 2 to m-1
end
a(:,:,m) = zeros(m-1); % Sub-matrix with index = m
%% Choice of indexes x and y where (x + y = m)
idx = get_random_index(m);
%% Replacement of the sub-matrices with indexes 1,2,...m in Hb
H1 = zeros(m*(m-1));
a = num2cell(a,[1 2]);
for ii = 1:m
for jj = 1:m
if ismember(Hb(ii,jj),idx)
H1((m-1)*(ii-1)+1:(m-1)*ii,(m-1)*(jj-1)+1:(m-1)*jj) = a{Hb(ii,jj)};
end
end
end
% Desired code rate
code_rate = 3/4;
% Determine the number of extra rows and columns needed
num_extra_rows = round((1/code_rate - 1) * m);
num_extra_cols = num_extra_rows;
% Create the extra rows and columns
extra_rows = repmat(eye(m), num_extra_rows, 1);
extra_cols = repmat(eye(m), 1, num_extra_cols);
% Add the extra rows and columns to H1
H1
extra_cols
%% Function
function idx = get_random_index(m)
if mod(m,2)
idx = randi([1 m-1]);
else
idx = randi([1 m-2]);
if idx >= m/2
idx = idx+1;
end
end
idx = [idx m-idx];
end
Afluo Raoual
on 2 May 2023
Dyuman Joshi
on 3 May 2023
What should be the final size of H1?
Afluo Raoual
on 3 May 2023
Dyuman Joshi
on 3 May 2023
"We can't specify the final size of H1_modified because the size depends on the code rate."
Then what is the relation of final size of H1 with code rate?
Answers (1)
Jon
on 2 May 2023
On line 34 H1 has m*(m-1) rows, but extra_cols only has m rows. So you can not horizontally concatenate H1 and extra_cols using
H1_modified = [H1 extra_cols];
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!