Error using horzcat Dimensions of arrays being concatenated are not consistent

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 Kommentare

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
H1 = 12×12
1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0
extra_cols
extra_cols = 4×4
1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1
%% 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
@Dyuman Joshi The aim is to construct a square regular matrix H1, then we add columns to this matrix to construct matrix with specific code rate. The matrix H1_modified must still regular (number of ones in each columns is equal and the number of ones in each row is equal) whenn adding columns
What should be the final size of H1?
@Dyuman Joshi We can't specify the final size of H1_modified because the size depends on the code rate. For example H1 is always square, and it depends on the value of m. Then, we add columns to this square matrix H1 in order to get the final matrix H1_modified which must be regular with the desired code rate.
"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?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

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];

Kategorien

Tags

Gefragt:

am 2 Mai 2023

Bearbeitet:

am 5 Mai 2023

Community Treasure Hunt

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

Start Hunting!

Translated by