Can't seem to replace values in matrix meeting criteria, although the correct row,col coordinates are found

1 view (last 30 days)
I have an 1000 x 12 "image" matrix I initialize to 0 corresponding to current (as in amps) values I calculated from a 2-dim array of voltages and impedances. I then look for values exceeding some number, and replace those with 1. The intent is to display the matrix as an image, with two colors indicating good and bad.
The problem is that although it seems I find the "right" coordinates where the current exceeded the threshold, I can't seem to be able to "populate" the correct ones in the matrix, using the same row,col coordinates returned.
clear;
vbat = [250:50:800];
ImaxGA = 150 ; % Arms
ImaxVA = 120; % Arms
zmutual =[0.001:.01:10]'
vbat2=repmat(vbat,size(zmutual,1),1);
zmutual2 = repmat(zmutual,1,size(vbat,2))
IGA=vbat2./zmutual2;
[row,col]=find(IGA>ImaxGA);
img = [zeros(size(zmutual,1),size(vbat,2))]
badreg=[zmutual(row) vbat(col)' row col]
%badregmapd = [int64((badreg(:,1)-.001)/.01+1),(badreg(:,2)-250)/50+1]
img(row,col)=1
map = [ 0 1 0; 1 0 0];
imagesc(img)
colormap(map)
set(gca,'YDir','normal')
xticks([1 2 3 4 5 6 7 8 9 10 11 12]);
xticklabels({'250' '300' '350' '400' '450' '500' '550' '600' '650' '700' '750' '800'})
yticks([100 200 300 400 500 600 700 800 900 1000])
yticklabels({'1','2','3','4','5','6','7','8','9','10'})
only the row,col entries in the img matrix should be replaced with 1. However, I see that (500, 1) (among others) is also replaced, even though row,col = (500, 1) was not part of the returned values. it seems like it is replacing every row,col combination.
How to I just replace the row,col pairs found?
Thank you!

Accepted Answer

Stephen23
Stephen23 on 3 May 2024
Edited: Stephen23 on 3 May 2024
Use SUB2IND:
idx = sub2ind(size(img),row,col);
img(idx) = 1;
The section "Indexing Matrices with Two Subscripts" here:
explains what your approach is actually doing (hint: it does not operate over pairs of subscripts as you think).

More Answers (0)

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!