I’m trying to run a loop through some measurement but I keep getting this error "Unable to perform assignment because the size of the left side is 3-by-1 and the size of the right side is 2-by-1." Does anyone know a way around this?
o = B(1:50,2);
for ind = 1:length(o)
a1(:,ind) = diff(o);
[c1(:,ind),d1(:,ind)] = find(a1(:,ind)>2^15);
[e1(:,ind),f1(:,ind)] = find(a1(:,ind)<-2^15);
o(c1(:,ind)+1:e1(:,ind)) = o(c1(:,ind)+1:e1(:,ind))-2^16; % Error Happens here
end

4 件のコメント

dpb
dpb 2019 年 7 月 14 日
The way around it is to not assign to a specific sequence other than a matching sequence.
What are you trying to do here? It's quite difficult to decide with meaningless variable names and no comments.
NB1: diff(o) is already the difference of the full vector o; there's no point in having that in the loop.
o=B(1:50,2); % a subset of B?
a1=diff(o); % first difference
igt=a1>2^15; % logical vector of differences >
imn=a1<2^15; % logical vector of < differences
NB2: Unless there's some guarantee placed on the form of the data, in general there would be no guarantee that numel()==TRUE in igt and imn would be the same or any particular number so indexing with them seems fraught with the likelihood of type of error you got.
Question is, what is the intent next???
Nikolaos Zafirakis
Nikolaos Zafirakis 2019 年 7 月 14 日
This is the code not placed in a loop. The issue is that I have some ready measurements, but they are not in the format I need them, thus I use this code to fix them but I need to run it multiple times to fix the numbers. That’s why I need the loop to work. Additionally, I need the diff to run each time in order to grab the new first value of interest.
o = B(1:50,2);
a1 = diff(o);
[c1,d1] = find(a1>2^15);
[e1,f1] = find(a1<-2^15);
if c1 > 0
o(c1+1:e1) = o(c1+1:e1)-2^16;
else c1 < 0
o(c1+1:e1) = o(c1+1:e1)+2^16;
end
if isempty(e1)
o(c1+1:end) = o(c1+1:end)-2^16;
end
dpb
dpb 2019 年 7 月 14 日
No idea what you're trying to explain, sorry.
Show us some data that illustrates what you're after with inputs and expected outputs and how you know those are the right answers given the input.
What is the end starting format and then the "the format I need" for the data? Bound to be a more effective way to code this if we just knew what the problem was/is...
Nikolaos Zafirakis
Nikolaos Zafirakis 2019 年 7 月 14 日
Original data
plot(a).jpg
The result after 2 iterations of the code i showed you (I want to run what I showed you in a loop).
plotb.jpg

サインインしてコメントする。

回答 (1 件)

SaiDileep Kola
SaiDileep Kola 2019 年 7 月 17 日

0 投票

Hi,
I see that you get the error in 3rd line in the for loop not in the of 4rth line as you mentioned, I think your use case can be realized with the following code.
o = B(1:50,2);
for ind = 1:length(o)
a1 = diff(o);
c1 = find(a1>2^15);
e1 = find(a1<-2^15); %Error happens here
o(c1+1:e1) = o(c1+1:e1)-2^16; % Error doesn't occur here
End

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

2019 年 7 月 14 日

回答済み:

2019 年 7 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by