Effacer les filtres
Effacer les filtres

could anyone help me how to reshape the matrix.

1 vue (au cours des 30 derniers jours)
jaah navi
jaah navi le 11 Mai 2019
Commenté : John D'Errico le 12 Mai 2019
I am having two matrices A=[3x2] and B=[1x2]
i want to reshape the size of A such that it should be equal to B,which mean A should contain only the first row.
could anyone please help me on this.

Réponse acceptée

John D'Errico
John D'Errico le 11 Mai 2019
Modifié(e) : John D'Errico le 11 Mai 2019
You are not asking to do a reshape, which does not change the total number of elements.
A = A(1,:);
  2 commentaires
jaah navi
jaah navi le 12 Mai 2019
I tried with the following code:
B=[ 0.7684 0.0120;
0.5333 0.9666;
0.0684 0.8322;
0.1996 0.4453]
A=[0.6399 0.8965]
if size(A)>size(B)
A=A(size(B,1),:)
social=B-A
else
[jj,kk]=size(A)
[jjj,kkk]=size(B)
zz=zeros(jjj,kkk)
zz(1:jj,1:kk)=A
social=B-zz
end
when i run the code it executes.
But when i run the code by changing A to B and B to A i am getting error for the following code:
A=[ 0.7684 0.0120;
0.5333 0.9666;
0.0684 0.8322;
0.1996 0.4453]
B=[0.6399 0.8965]
if size(A)>size(B)
A=A(size(B,1),:)
social=B-A
else
[jj,kk]=size(A)
[jjj,kkk]=size(B)
zz=zeros(jjj,kkk)
zz(1:jj,1:kk)=A
social=B-zz
end
Could you please help me to solve the issue.I want to form the code in such a way the code needs to be executed when size(A) > size(B) and when size(B)> size(A)
John D'Errico
John D'Errico le 12 Mai 2019
Your problem is that you do not understand how if statements work in MATLAB, or you do not understand what size returns.
B=[ 0.7684 0.0120;
0.5333 0.9666;
0.0684 0.8322;
0.1996 0.4453]
A=[0.6399 0.8965]
Now, what are size(A) and size(B)?
size(A)
ans =
1 2
size(B)
ans =
4 2
Now, what happens when you use if?
help if
if Conditionally execute statements.
The general form of the if statement is
if expression
statements
ELSEIF expression
statements
ELSE
statements
END
The statements are executed if the real part of the expression
has all non-zero elements.
So, what hapens when you try the statement:
if size(A) > size(B)
What does that comparison return?
size(A) > size(B)
ans =
1×2 logical array
0 0
Does the REAL part of that result have ALL non-zero elements?
Instead, you might want to use the second argument to size. For example, do a comparison like this:
if size(B,1) > size(A,1)
disp('IT WORKS!')
else
disp('The sky is falling.')
end
IT WORKS!
So when B had more rows in it than A, the if branch executes. See what happens when we change the if clause to not use the second argument to size, even though we know that B indeed is a "larger" array than A, that is, it has more rows than A.
if size(B) > size(A)
disp('IT WORKS!')
else
disp('The sky is falling.')
end
The sky is falling.
So the primary branch in the if failed to execute, dropping down into the else clause.
The point is, you need to understand how the if statement works. You need to understand size.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Large Files and Big Data dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by