Using a 3D matrix to index a Cell Array without For Loops

Hi
I have a 3D matrix (lets call it A) which indexes a cell array (called B). A stores the location of positions to access in B in the form of [cell, row, column]. I.e A(1,1,1) would reference cell 1, row 1 column 1 in B. However, A is very large, where row and column are in the thousands. Is there a way I can access all the locations of B (and store the value at that point) without using nested for loops (which takes a long time)?
Thanks

14 件のコメント

Stephen23
Stephen23 2019 年 9 月 10 日
"A(1,1,1) would reference cell 1, row 1 column 1 in B"
This means that the values of A are totally irrelevant. Rather strange.
"I have a 3D matrix (lets call it A) ... However, A is very large, where row and column are in the thousands.."
And what size is its third dimension?
It would probably help if you uploaded some sample data in a .mat file.
Joshua Monteiro
Joshua Monteiro 2019 年 9 月 10 日
Hey, sorry about that; the 3rd dimension has a size of 3 (as it stores RGB data). The array called location stores the location of the desired position in the cell array called Images.IMG_20190911_015734.jpg
Joshua Monteiro
Joshua Monteiro 2019 年 9 月 10 日
The array which stores these values is called newImages and for the sample data above, should look like thisIMG_20190911_015753.jpg
Stephen23
Stephen23 2019 年 9 月 10 日
@Joshua Monteiro: sorry, I can't load matrices which are shown in images.
If you want help, please upload some sample data in a .mat file.
Guillaume
Guillaume 2019 年 9 月 10 日
Don't post massive screnshots. Write your matrices as code in your question (eg. see below), or attach a mat file.
Location = cat(3, [1, 3; 4, 2], [1, 1; 2, 2], [1, 2; 2, 1])
It's unclear what location refers to.
Joshua Monteiro
Joshua Monteiro 2019 年 9 月 10 日
編集済み: Stephen23 2019 年 9 月 10 日
Here you are; many thanks
Guillaume
Guillaume 2019 年 9 月 10 日
編集済み: Guillaume 2019 年 9 月 10 日
Right, so we have two matrices and one cell array of 4 matrices, all 2x2x3.
What is the relationship between them and what do you want to do?
Joshua Monteiro
Joshua Monteiro 2019 年 9 月 10 日
The matrix location is used to index Images (they are always the same size). The dimensions of location refer to [cell, row, col] of Images. As the pictures show (they provide a better description of what I want than the .mat uploads), I'd like to use locations to access Images (and take out the RGB values which [cell, row, col] points to all at the same time (withOUT using nested for loops to step through each value in turn). The RGB values extracted from Images are used to create a new matrix called newImage. The uploaded newImage matrix shows the desired result.
Thank you
Bruno Luong
Bruno Luong 2019 年 9 月 10 日
Do you have a code with for-loop to illustrate what should be done?
Bruno Luong
Bruno Luong 2019 年 9 月 10 日
編集済み: Bruno Luong 2019 年 9 月 10 日
and A(i,j,k) contains what? I guess it related to findMax but you never tell it clearly.
B in your first post is Image?
Joshua Monteiro
Joshua Monteiro 2019 年 9 月 10 日
My apologies for the confusion.
A(i,j,k) WAS location (I've changed the function slightly) but it's pretty much split into
(cell, row, column).
findMax specifies the cell
row specifies the row
col specifies the column
B refers to Image in the supplied code
Bruno Luong
Bruno Luong 2019 年 9 月 10 日
編集済み: Bruno Luong 2019 年 9 月 10 日
A(i,j,k) WAS location (I've changed the function slightly) but it's pretty much split into
(cell, row, column)
For a scalar triplets (i,j,k), A(i,j,k) is a single value (it cannot be then (cell, row, column)). You say "location" but what LOCATION???
  • i is in what range?
  • j is in what range?
  • k is in what range?
  • A(i,j,k) is in what range?
I guess A(i,j,k) is a CELL location. But as you says (cell, row, column) it confuses people (me) more than anything else.
Joshua Monteiro
Joshua Monteiro 2019 年 9 月 10 日
I'm sorry but what you mean by the range?
Bruno Luong
Bruno Luong 2019 年 9 月 10 日
編集済み: Bruno Luong 2019 年 9 月 10 日
range := (1:x), where x is size(zzz,y), with some (other) variable zzz, the interval where the (index) variable should belong to. Example:
A = randn(3,4,5)
when I ask range specification different parameters in A(i,j,k)
  • range of i is 1:3
  • range of j is 1:4
  • range of k is1:5
  • range of A(:,:,:) is (-Inf,inf)
This allows us to understand the relationship of A and other variables.

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

回答 (1 件)

Bruno Luong
Bruno Luong 2019 年 9 月 10 日
編集済み: Bruno Luong 2019 年 9 月 10 日

0 投票

Just a guess
load('Images.mat')
load('location.mat')
fileImages=cat(4,fileImages{:});
[m,n,p]=size(locationMax);
[I,J,K]=ndgrid(1:m,1:n,1:p);
NewImage=fileImages(sub2ind(size(fileImages),I,J,K,locationMax));

1 件のコメント

Joshua Monteiro
Joshua Monteiro 2019 年 9 月 10 日
編集済み: Joshua Monteiro 2019 年 9 月 11 日
A very good guess indeed, especially since I was vague. The code runs, but it didn't produce the desired result. However, I found it quite clever how you stored all the images (essentially the Z direction) using a 4th dimension. Using that trick, and modifying the code, it now works. Thank you.

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

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

製品

質問済み:

2019 年 9 月 10 日

編集済み:

2019 年 9 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by