mis match matrix dimension

5 visualizaciones (últimos 30 días)
Life is Wonderful
Life is Wonderful el 28 de Mzo. de 2023
Comentada: Life is Wonderful el 29 de Mzo. de 2023
Hi
Consider the example and propose a solution to the dimension mismatch.
Thank you very much
a = zeros(15,41);
b = ones(1,2);
for i = 1:length(a)
a(:,i) = b;
end
Unable to perform assignment because the size of the left side is 15-by-1 and the size of the right side is 1-by-2.
  6 comentarios
Dyuman Joshi
Dyuman Joshi el 28 de Mzo. de 2023
Editada: Dyuman Joshi el 28 de Mzo. de 2023
"The issue is that after the third iteration, the b matrix is surpassed."
Can you explain what do you mean this?
"How to transform a matrix 1 x 2 to a 15 x 41 ?"
Since the dimensions of b are not multiple of the dimensions of a, it is not possible to transform (or replicate in any manner) b into a. Though you can replicate to nearest dimension and crop the required size.
a = zeros(15,41);
b = [1 2];
What is the expected output for the above data?
Life is Wonderful
Life is Wonderful el 29 de Mzo. de 2023
You can see how matrix dimension transformed from the article by @Bjorn Gustavsson

Iniciar sesión para comentar.

Respuesta aceptada

Bjorn Gustavsson
Bjorn Gustavsson el 28 de Mzo. de 2023
As the commenters have indicated your request is to non-specific to have one solution, while we all expect you to have one specific solution in mind. Therefore it is unlikely that the solutions we suggest will be the one you have in mind. You could do something like this:
a = zeros(15,41);
b = ones(1,2);
for i = 1:size(a,2) % It is better to use the size of a in the dimension you use it for, this makes the code robus and will work even in the case a is larger along the first dimension
a(:,i) = b(1+rem(i,2)); % This will copy the elements in b into a i some order, probably not the one you have in mind, but perhaps?
end
You could also do something like:
a = zeros(15,41);
b = ones(1,2);
for i2 = 1:2:size(a,2), % another programming trick is to name the loop-variables to indicate their meaning, here i2 - since we're looping over the second dimension of a
a(:,i2:(i2+1)) = repmat(b,size(a,1),1);
end
Since both elements of b are identical you could do something like this:
a(:) = b(1); % this will work for this specific case, but not when b(2) ~= b(1)
Your question indicate that you are very new to matlab-use. To get up to speed as efficiently and rapidly as possible I've come to understand that the on-ramp course/material is good - that is the Mathworks introduction to matlab and designed for this purpose.
HTH
  1 comentario
Life is Wonderful
Life is Wonderful el 28 de Mzo. de 2023
Editada: Life is Wonderful el 29 de Mzo. de 2023
Perfect method to show how a question can be analysed; option 2 will solve the problem for me.
Thanks a lot for your help

Iniciar sesión para comentar.

Más respuestas (1)

Nithin Kumar
Nithin Kumar el 28 de Mzo. de 2023
Editada: Nithin Kumar el 28 de Mzo. de 2023
Hi,
I understand that you are trying to assign matrix "b" to matrix "a" where the order of matrices are not the same. You can apply assignment operation on matrices when they are of the same order.
Additionally, you can apply assignment operation on matrices if at least one of the two dimensions of both the matrices are equal using concatenation.
For more information on the matrix dimensions, kindly refer the following link Creating, Concatenating, and Expanding Matrices - MATLAB & Simulink - MathWorks India
I hope this answer resolves the issue you are encountering.
  1 comentario
Life is Wonderful
Life is Wonderful el 28 de Mzo. de 2023
Editada: Life is Wonderful el 29 de Mzo. de 2023
I appreciate you helping me.
This is vague and doesn't support the dimension resolution when they're distinct.

Iniciar sesión para comentar.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by