Info

This question is closed. Reopen it to edit or answer.

Unable to perform assignment because left and right sides have different number of elements

1 view (last 30 days)
I am trying to separate a data matrix into multiple separate matrices between zeros, and name them accordingly (e.g., the first "range" of data before the first zero is named "range[1]", the next is named "range[2]", etc.). I was able to do this by storing them in cells, but I need them to be in matrices to do further analysis afterwards, so I am hoping that there is a way to do this separation without converting to cells...any suggestions?
This is my code thus far:
clear all;
close all;
data = [1.431294118; 1.251294118; 1.121294118; 0.051294118; 0.461294118; 0.971294118; 0.481294118; 20.01129412; 14.84870588;13.13129412;0.718705882;0.401294118;0.331294118;0.098705882;0.731294118;0.701294118;0.861294118;0.358705882;0.521294118;0.291294118;0.138705882;11.72129412;7.618705882;2.528705882;1.611294118;0.401294118;0.601294118;0.121294118;0.051294118;0.348705882;0.211294118;0.241294118;0.168705882;0.218705882;4.981294118;15.22870588;2.028705882;0.621294118;1.391294118;1.001294118;0.131294118;1.101294118;0.081294118;0.488705882;1.321294118;];
thresh_data = 1.4;
data(data < thresh_data) = 0;
array_length = length(data);
sample = array_length;
k = 1;
for i = 1:array_length
if (data(i) ~= 0)
range(k) = [range(k), [data(i)]];
elseif (data(i) == 0)
k = k + 1;
end
end

Answers (1)

Jan
Jan on 7 Jun 2019
Edited: Jan on 7 Jun 2019
range(k) = [range(k), [data(i)]];
This cannot work, because range(k) is a scalar, but [range(k), [data(i)]] has 2 elements.
By the way, there is no need for square brackets around data(i).
I do not know, what "the first zero is named "range[1]"" means. Maybe you mean a cell array:
k = 1;
range = {};
for i = 1:array_length
if data(i) ~= 0
range{k} = {[range{k}, data(i)]};
else % if data(i) == 0
k = k + 1;
end
end
If this is wanted, there are more efficient methods.
  1 Comment
Wes Anderson
Wes Anderson on 7 Jun 2019
I guess a better way to word my question would be "how can I separate one matrices into separate matrices that are named differently and accessable by looping through the original matrices?". The separation occurs at values between zeros...e.g. A = [0 0 1 2 4 2 0 0 0 3 6 7 0 0] becomes range[1] = [1 2 4 2] and range [2] = 3 6 7]....is there a way to do this without converting to a cell array? Thank you!

Community Treasure Hunt

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

Start Hunting!