for i = 1 :1000
a(:,i) = b ;% assume b size 800 X 1 vector
% but b size changes at random iteration (for loop) depends on the problem i mean it size
% decrease so how to store b values in a as it showing error
end
Error:
Unable to perform assignment because the size of the left side is 1-by-800 and
the size of the right side is 797-by-1.
Other case :
for i = 1 :1000
a(:,i) = b'; % assume b size 800 X 1 vector
% but b size changes at random iteration (for loop) depends on the problem i mean it size
% decrease so how to store b values in a as it showing error
end
Unable to perform assignment because the indices on the left side are not
compatible with the size of the right side.
I can understand what error showing but how to tackle this problem. Any help appreciated .
Thanks in advance.

댓글 수: 4

Geoff Hayes
Geoff Hayes 2022년 1월 11일
@RAKESH KUMAR TOTA - can a be a cell array instead since the b at each iteration can be of different sizes?
RAKESH KUMAR TOTA
RAKESH KUMAR TOTA 2022년 1월 11일
I have no issue 'a' can be of any data type . I need to use 'a' further to investigate and plot results. thanks
VBBV
VBBV 2022년 1월 11일
Can you paste the actual error that you faced. Everything in red color text shown at command window
RAKESH KUMAR TOTA
RAKESH KUMAR TOTA 2022년 1월 11일
Unable to perform assignment because the size of the left side is 1-by-800 and the size of the right side is 797-by-1. for first case type
Unable to perform assignment because the indices on the left side are not
compatible with the size of the right side. for second case type

댓글을 달려면 로그인하십시오.

 채택된 답변

KSSV
KSSV 2022년 1월 11일

0 개 추천

When you are not sure of size b, you can save them into a cell array.
a = cell(1000,1) ;
for i = 1 :1000
a{i} = b ;
end
You can access any cell using a{1}, a{2},....a{i},...a{1000}.

댓글 수: 5

RAKESH KUMAR TOTA
RAKESH KUMAR TOTA 2022년 1월 11일
Sorry i forgot to mention i need to store 'a' for intance of size 800X1 at every iteration of for loop even though b size decreasing by assigning all zeros (decreased size) to the rest of 'a' compensating to maintain original size (800 X1). Actually in 'b' certain position become zero we eliminate in the next iteration and store it in 'a' but 'a' size should not change.
I appreciate your response thats the good way to represent to assign 'b' to 'a' so easy to convert using formulae cell2mat for further analysis.
Any idea in this direction please.
If the size of the b is always less than or equal 800x1....you may follow the below:
a = zeros(800,1000) ;
for i = 1 :1000
temp = b ;
a(1:length(b),i) = b ;
end
Thanks a lot.
Just a question.
Is it better to use
a = []; % or
a = zeros(800,10000); % if we dont know exactly how many iteration it will take
% for better programming.
KSSV
KSSV 2022년 1월 11일
Initializing with dimensions is suggested.
RAKESH KUMAR TOTA
RAKESH KUMAR TOTA 2022년 1월 11일
okay thank you.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

릴리스

R2021b

질문:

2022년 1월 11일

댓글:

2022년 1월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by