How to iteratively append elements to an array

조회 수: 3 (최근 30일)
Abdul Rahim Mohammad
Abdul Rahim Mohammad 2019년 4월 9일
댓글: Abdul Rahim Mohammad 2019년 4월 9일
I = imread(chart_image);
R = I(:,:,1);
G = I(:,:,2);
B = I(:,:,3);
N= zeros(24,3);
final_chart =zeros (24,3);
Vals = [175 175; 425 175; 650 175; 925 175;
175 420; 425 420; 650 420; 925 420;
175 670; 425 670; 650 670 ; 925 670 ;
175 925; 425 925; 650 925; 925 925;
175 1170; 425 1170; 650 1170; 925 1170;
175 1425; 425 1425 ; 650 1425 ; 925 1425];
for i = 1:24
N(i)=[R(Vals(i,1),Vals(i,2)) G(Vals(i,1),Vals(i,2)) B(Vals(i,1),Vals(i,2))];
final_chart % the variable I want to modify to be able to store the vales of all N as it iterates over the 24 values in Vals into a single 24x3 matrix
end
hello, I wanted to add the value of each N to a final array called final_chart, but I keep getting error "Unable to perform assignment because the left and right sides have a different number of elements.", I've tried other ways to append but for some reason I keep failing all of those. Can someone help me understand where I'm going wrong and help me fix my code. Thank you.
  댓글 수: 2
Guillaume
Guillaume 2019년 4월 9일
I don't see how you can get the error "Unable to perform assignment because the left and right sides have a different number of elements" from the code you've posted, since there's no indexing on the left-hand side of any the assignment lines. This error would only occur if you'd written
N(i, :) = ...
%not N(i) = ...
It's not clear what you're trying to do. Your current code overwrites N at each step, so only the value calculated on the last step is retained.
Abdul Rahim Mohammad
Abdul Rahim Mohammad 2019년 4월 9일
Apoligies, let me edit the question to make it more clear.

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

채택된 답변

Guillaume
Guillaume 2019년 4월 9일
If I understood what you're trying to do, there's no need for the loop:
I = imread(chart_image);
R = I(:,:,1);
G = I(:,:,2);
B = I(:,:,3);
Vals = [175 175; 425 175; 650 175; 925 175;
175 420; 425 420; 650 420; 925 420;
175 670; 425 670; 650 670 ; 925 670 ;
175 925; 425 925; 650 925; 925 925;
175 1170; 425 1170; 650 1170; 925 1170;
175 1425; 425 1425 ; 650 1425 ; 925 1425];
locations = sub2ind(size(R), Vals(:, 1), Vals(:, 2));
N = [R(locations), G(locations), B(locations)];
You don't even need to separate the image into colour planes:
I = imread(chart_image);
[width, height, ~] = size(I);
Vals = [175 175; 425 175; 650 175; 925 175;
175 420; 425 420; 650 420; 925 420;
175 670; 425 670; 650 670 ; 925 670 ;
175 925; 425 925; 650 925; 925 925;
175 1170; 425 1170; 650 1170; 925 1170;
175 1425; 425 1425 ; 650 1425 ; 925 1425];
locations = sub2ind([width, height], Vals(:, 1), Vals(:, 2));
N = I(locations + (0:2) * width * height);
  댓글 수: 3
Guillaume
Guillaume 2019년 4월 9일
N(i) = [array of 3 elements]
Is indeed going to result in an error since you're trying to put 3 numbers into just one slot.
N(i, :) = ...
would have worked (for N preallocated with 3 columns as you have done).
Abdul Rahim Mohammad
Abdul Rahim Mohammad 2019년 4월 9일
Thank you, that was very helpful. I was having a hard time understading what was going wrong but I see now that I've messed up at N(i).

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by