could anyone help me how to reshape the matrix.

1 view (last 30 days)
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.

Accepted Answer

John D'Errico
John D'Errico on 11 May 2019
Edited: John D'Errico on 11 May 2019
You are not asking to do a reshape, which does not change the total number of elements.
A = A(1,:);
  2 Comments
jaah navi
jaah navi on 12 May 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 on 12 May 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.

Sign in to comment.

More Answers (0)

Categories

Find more on Large Files and Big Data in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!